Giter Site home page Giter Site logo

kjonathante / ucbsf-express-react-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 5.0 41 KB

Boilerplate for creating an express.js backend and a react frontend application.

Home Page: https://ucbsf-express-react-boilerplat.herokuapp.com/

JavaScript 36.70% HTML 54.69% CSS 8.61%

ucbsf-express-react-boilerplate's Introduction

Table of Contents

General Setup

The usual

  1. Create repo in Github
  2. Clone repo
  3. CD to repo folder

Create server and client folder

mkdir server client

Run npm init

npm init -y

Edit package.json

{
  "name": "react-ucbsf",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "install": "cd client && npm install",
    "heroku-postbuild": "cd client && npm run build",
    "start": "node server"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

Create .gitignore file

# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
node_modules
.pnp
.pnp.js

# testing
coverage

# production
build

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

package-lock.json
yarn.lock

Backend Setup

Create index.js inside the server folder

const path = require('path')
const express = require('express')
const data = require('./db/fakedata.json')
const app = express()

if (process.env.NODE_ENV === 'production') {
  app.use(express.static(path.join(__dirname, '../client/build')))
}

app.get("/api/all", (req, res) => {
  res.json(data)
})

if (process.env.NODE_ENV === 'production') {
  app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname,'../client/build/index.html'))
  })
}

const PORT = process.env.PORT || 3001
app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`)
})

Create db folder inside server

mkdir server/db

Create dummy data inside the db folder

{
  "items": {
    "item": [
      {
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55,
        "batters": {
          "batter": [
            {
              "id": "1001",
              "type": "Regular"
            },
            {
              "id": "1002",
              "type": "Chocolate"
            },
            {
              "id": "1003",
              "type": "Blueberry"
            },
            {
              "id": "1004",
              "type": "Devil's Food"
            }
          ]
        },
        "topping": [
          {
            "id": "5001",
            "type": "None"
          },
          {
            "id": "5002",
            "type": "Glazed"
          },
          {
            "id": "5005",
            "type": "Sugar"
          },
          {
            "id": "5007",
            "type": "Powdered Sugar"
          },
          {
            "id": "5006",
            "type": "Chocolate with Sprinkles"
          },
          {
            "id": "5003",
            "type": "Chocolate"
          },
          {
            "id": "5004",
            "type": "Maple"
          }
        ]
      },
    ]
  }
}

Frontend Setup

CD into client

cd client

Run create-react-app

npx create-react-app .

source:

  1. fetch - https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
  2. create-react-app - https://create-react-app.dev/
  3. require json - https://blog.codingblocks.com/2018/reading-json-files-in-nodejs-require-vs-fs-readfile/
  4. json sample - https://opensource.adobe.com/Spry/samples/data_region/JSONDataSetSample.html

ucbsf-express-react-boilerplate's People

Contributors

kjonathante avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.