How to do it...

Firstly, create a file named server.js that will include two middleware functions. One that configures a session and the other that makes sure that there is a connection to the MongoDB before allowing any route to be called. Then, we mount our API routes to a specific path:

  1. Create a new file named server.js
  2. Include the required libraries. Then, initialize a new ExpressJS application and create a connection to MongoDB:
 const mongoose = require('mongoose') const express = require('express') const session = require('express-session') const bodyParser = require('body-parser') const MongoStore = require('connect-mongo')(session) const api = require('./api/controller') const app = express() const db = mongoose.connect( 'mongodb://localhost:27017/test' ...

Get MERN Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.