Express React Build
In this build we will
- Build an Express API
- Use Mongo/Mongoose with 1 model
- Deploy the Api with Heroku
- Build a Full Crud Frontend with React
- Deploy with Netlify
Setup for Express Build
- Create a folder called express-react
- Inside this folder create another folder called backend
- Generate a React app called frontend
npx create-react-app frontend
Your folder structure should look like this...
/express-react
-> /backend
-> /frontend
- cd into
backend
folder
Setting up the Express app
- create a new node project
npm init -y
- install dependencies
npm install dotenv mongoose express cors morgan
- install dev dependencies
npm install --save-dev nodemon
- setup npm scripts
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
}
- make files
touch .env .gitignore server.js
- put the following .gitignore
/node_modules
.env
- put the following in .env (make sure to use YOUR mongodb.com uri)
MONGODB_URL=mongodb+src://...
PORT=4000
Starting Server.js
Let's build out the minimum to get server.js up and running
///////////////////////////////
// DEPENDENCIES
////////////////////////////////
// get .env variables
require("dotenv").config();
// pull PORT from .env, give default value of 3000
const { PORT = 3000 } = process.env;
// import express
const express = require("express");
// create application object
const app = express();
///////////////////////////////
// ROUTES
////////////////////////////////
// create a test route
app.get("/", (req, res) => {
res.send("hello world");
});
///////////////////////////////
// LISTENER
////////////////////////////////
app.listen(PORT, () => console.log(`listening on PORT ${PORT}`));
- run the server
npm run dev
and make sure you see "Hello World" when you go tolocalhost:4000
Adding a Database Connection
Let's update our server.js to include a database connection
///////////////////////////////
// DEPENDENCIES
////////////////////////////////
// get .env variables
require("dotenv").config();
// pull PORT from .env, give default value of 3000
// pull MONGODB_URL from .env
const { PORT = 3000, MONGODB_URL } = process.env;
// import express
const express = require("express");
// create application object
const app = express();
// import mongoose
const mongoose = require("mongoose");
///////////////////////////////
// DATABASE CONNECTION
////////////////////////////////
// Establish Connection
mongoose.connect(MONGODB_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
// Connection Events
mongoose.connection
.on("open", () => console.log("Your are connected to mongoose"))
.on("close", () => console.log("Your are disconnected from mongoose"))
.on("error", (error) => console.log(error));
///////////////////////////////
// ROUTES
////////////////////////////////
// create a test route
app.get("/", (req, res) => {
res.send("hello world");
});
///////////////////////////////
// LISTENER
////////////////////////////////
app.listen(PORT, () => console.log(`listening on PORT ${PORT}`));
- Make sure you see the Mongoose Connection message when the server restarts
Adding the People Model
Let's add a People model to server.js along with an index and create route to see and create our people. Make sure to add cors and express.json middleware!
///////////////////////////////
// DEPENDENCIES
////////////////////////////////
// get .env variables
require("dotenv").config();
// pull PORT from .env, give default value of 3000
// pull MONGODB_URL from .env
const { PORT = 3000, MONGODB_URL } = process.env;
// import express
const express = require("express");
// create application object
const app = express();
// import mongoose
const mongoose = require("mongoose");
// import middlware
const cors = require("cors");
const morgan = require("morgan");
///////////////////////////////
// DATABASE CONNECTION
////////////////////////////////
// Establish Connection
mongoose.connect(MONGODB_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
// Connection Events
mongoose.connection
.on("open", () => console.log("Your are connected to mongoose"))
.on("close", () => console.log("Your are disconnected from mongoose"))
.on("error", (error) => console.log(error));
///////////////////////////////
// MODELS
////////////////////////////////
const PeopleSchema = new mongoose.Schema({
name: String,
image: String,
title: String,
});
const People = mongoose.model("People", PeopleSchema);
///////////////////////////////
// MiddleWare
////////////////////////////////
app.use(cors()); // to prevent cors errors, open access to all origins
app.use(morgan("dev")); // logging
app.use(express.json()); // parse json bodies
///////////////////////////////
// ROUTES
////////////////////////////////
// create a test route
app.get("/", (req, res) => {
res.send("hello world");
});
// PEOPLE INDEX ROUTE
app.get("/people", async (req, res) => {
try {
// send all people
res.json(await People.find({}));
} catch (error) {
//send error
res.status(400).json(error);
}
});
// PEOPLE CREATE ROUTE
app.post("/people", async (req, res) => {
try {
// send all people
res.json(await People.create(req.body));
} catch (error) {
//send error
res.status(400).json(error);
}
});
///////////////////////////////
// LISTENER
////////////////////////////////
app.listen(PORT, () => console.log(`listening on PORT ${PORT}`));
- create 3 people using postman to make post requests to /people
- test the index route with a get request to /people
Update and Delete
Let's add an Update and Delete API Route to server.js
///////////////////////////////
// DEPENDENCIES
////////////////////////////////
// get .env variables
require("dotenv").config();
// pull PORT from .env, give default value of 3000
// pull MONGODB_URL from .env
const { PORT = 3000, MONGODB_URL } = process.env;
// import express
const express = require("express");
// create application object
const app = express();
// import mongoose
const mongoose = require("mongoose");
// import middlware
const cors = require("cors");
const morgan = require("morgan");
///////////////////////////////
// DATABASE CONNECTION
////////////////////////////////
// Establish Connection
mongoose.connect(MONGODB_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
// Connection Events
mongoose.connection
.on("open", () => console.log("Your are connected to mongoose"))
.on("close", () => console.log("Your are disconnected from mongoose"))
.on("error", (error) => console.log(error));
///////////////////////////////
// MODELS
////////////////////////////////
const PeopleSchema = new mongoose.Schema({
name: String,
image: String,
title: String,
});
const People = mongoose.model("People", PeopleSchema);
///////////////////////////////
// MiddleWare
////////////////////////////////
app.use(cors()); // to prevent cors errors, open access to all origins
app.use(morgan("dev")); // logging
app.use(express.json()); // parse json bodies
///////////////////////////////
// ROUTES
////////////////////////////////
// create a test route
app.get("/", (req, res) => {
res.send("hello world");
});
// PEOPLE INDEX ROUTE
app.get("/people", async (req, res) => {
try {
// send all people
res.json(await People.find({}));
} catch (error) {
//send error
res.status(400).json(error);
}
});
// PEOPLE CREATE ROUTE
app.post("/people", async (req, res) => {
try {
// send all people
res.json(await People.create(req.body));
} catch (error) {
//send error
res.status(400).json(error);
}
});
// PEOPLE CREATE ROUTE
app.put("/people/:id", async (req, res) => {
try {
// send all people
res.json(
await People.findByIdAndUpdate(req.params.id, req.body, { new: true })
);
} catch (error) {
//send error
res.status(400).json(error);
}
});
// PEOPLE CREATE ROUTE
app.delete("/people/:id", async (req, res) => {
try {
// send all people
res.json(await People.findByIdAndRemove(req.params.id));
} catch (error) {
//send error
res.status(400).json(error);
}
});
///////////////////////////////
// LISTENER
////////////////////////////////
app.listen(PORT, () => console.log(`listening on PORT ${PORT}`));
Deploy
- create a git repo in the
backend
foldergit init
- add all files to staging
git add .
- commit
git commit -m "message"
- create a new repo on github.com (make sure its empty and public)
- add the remote to your local repo
git remote add origin URL
replate URL with your repos url - push up your code
git push origin branchName
replace branch name with your active branch, find that withgit branch
- go to heroku and create a new project
- under deploy connect your repo, enable auto deploys, and trigger a manual deploy
- under settings set your MONGO_URL config var
- in postman test all your API endpoints
Lab Part 1 - Cheese App
- create another folder called "Cheese App"
- create a backend and frontend folder like you did for today's lesson
- create a cheese API with index, create, update and delete routes
- the model should look like
name: String,
countryOfOrigin: String,
image: String
- Test the API, deploy the API, test the deployed API