touch server.js
npm init -y
npm install express mongoose
- Require express
- Create instance of express
- Set the server PORT to 3001
- Listen to the PORT
- Add middleware
app.use(express.urlencoded({ extended: true}));
app.use(express.json());
- Add routes
** Great time to test your server using Postman **
- Require mongoose in the server
- Setup the mongoose connection
- Add mongoose config object to the .connect method
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
}
- Add promise .then and .catch for Mongoose connection
npx create-react-app client
(always name client inside of MERN)- Add scripts in server package.json
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.js",
"start:dev": "concurrently \"nodemon -- ignore 'client/*'\" \"npm run client\",
"client": "cd client && npm run start"
npm install if-env
npm install concurrently nodemon -D
- run
npm start
at the root starts both servers - In the client package.json add a proxy:
"proxy": "http://localhost:3001",
git add .
->git commit -m
heroku create
- Add three more scripts to server package.json
"install": "cd client && npm install",
"build": "cd client && npm run build",
"heroku-postbuild": "npm run build"
- Add express static to serve up the build folder
app.use(express.static("client/build"));
- Add a wildcard route to serve up the client index.html
git push heroku main
- Add MONGODB_URI env var in Heroku settings