A SQL, Express, Node, React app.
It is designed to keep track of available Pizzas, display them to the user, and allow an admin to edit available pizzas.
Today, we're going to be building the REST endpoints on the server, and the database structure.
- Create a
server.js
file. - Run
npm init -y
- Npm install:
npm install express sequelize mysql2
- Require express
- Create an instance of express, called app.
- Create a PORT
- Listen on the PORT.
- Add body-parser middleware.
- Create a schema.sql file to drop and re-create database.
- Run the schema code in MySQL Workbench and confirm the database was successfully created. (Sequelize will handle our tables for us.)
** NOTE: Make sure you have sequelize-cli
installed globally or these commands will not work. **
- Run the command:
sequelize init:models & sequelize init:config
- Update config.json with your local password.
- Update config.json with your database name.
- Create a new file
user.js
- Build out your model.
- Go back to
server.js
and require "./models". - Sync our database.
db.sequelize.sync().then(() => {});
** NOTE: If you need to make a change, you will have to add {force:true}
to your db.sequelize.sync() method. **
- Build out any remaining models.
- Create a get route for
/api/config
in the server.js - Create a
controllers
folder. - Create
*Controller.js
file for each model. - Scaffold out the basic controller using
express.Router()
const express = require("express");
const router = express.Router();
module.exports = router;
- Build out 5 RESTful Routes
- GET all users
- GET single user by Id
- POST create a new user
- PUT update a user
- DELETE delete a user
- Require models folder.
- Call sequelize method
findAll
in my /api/users route. - Call sequelize method
create
in my /api/users POST route.
- Add a
watch
ordev
script to package.json that includesnodemon server.js
- Sequel injection attacks.
- Sanitize response bodies.
- Sequelize validations to handle password length and complexity.
- Run
create-react-app client
- Make sure that our server is running on a different port than React. (CRA runs on 3000 by default).
- Add a route for serving React in production to the
server.js
file. - Add express.static for serving up the generated build folder and files.
- Add boilerplate scripts for building and running both apps.