Node icon
Building a Production Ready Node.js JSON API
  •  

Initializing a Node Project

Learn how to get an Express.js application running in development using Nodemon and NPM

With the seed project cloned and MongoDB installed, let's go over what we currently have in our project, and everything we need to know to get our server running.

Anatomy of an Express Application

The seed project's directory structure is based off of the one created by the express-generator. This is one of the many ways in which you can structure an Express application, and you may opt to restructure your application however you like.

.
├── config/
│   └── index.js
├── models/
├── public/
├── routes/
│   ├── api/
│   │  └── index.js
│   └── index.js
├── app.js
├── package.json
└── .gitignore
  • The config folder will be used for storing configuration settings for our application. For our project, we'll be storing our environment variables and configuration for passport.js in this folder.
  • The models folder will be used for storing our Mongoose models. These models will contain the schema for our data and will be the entry point for how our data gets in and out of MongoDB.
  • The public folder is used for storing static files to be served, such as HTML, CSS, and Javascript files. Since we're only concerned about building an API, this folder won't be used in this tutorial, but if build a client application for this project later it can live in this folder.
  • The routes folder is where we define the routes that our application will respond to and will contain the logic for our endpoints.
  • app.js is the entry point into our application. This is the file that node will execute and will bring together all parts of our application (routes, models, etc).
  • The package.json file is used by npm for declaring our dependencies and scripts for our application. You can read more about the different configuration options that can go into package.json here

Starting the Server for the First Time

While using the node process is great for starting our server in production, it can often be a hassle in development since node doesn't watch our files for changes and reload them automatically. That means every time we make a change to a file we have to restart our server. Thankfully, there's a tool called nodemon that will automatically monitor the folder of our project and reboot our server for us whenever it sees a file change, which is perfect for our development environment.

Install nodemon

Since nodemon is a command line tool, it has to be installed as a global node package.

npm install -g nodemon

If the install command fails complaining about permissions, you may need to run the install command with sudo

Once nodemon is installed, we can boot our server by running nodemon app.js in our project's directory.

Boot up the Node server

First, make sure that MongoDB is already running in the background. You can test this by running mongo in your terminal. If you get a message saying "connect failed" that means MongoDB isn't running. Otherwise, if get a mongo shell, that means MongoDB is running in the background and mongo was able to connect to it successfully.

Next, run:

nodemon app.js

You should then see a message similar to the following:

[nodemon] starting `node app.js`
Listening on port 3000

Which means your server booted up successfully!

Using an NPM Script to Start the Server in Development

While using nodemon directly in the command line is sufficient enough to get our server running in development, it's common practice in Node to wrap your server startup commands as a NPM script. This gives greater flexibility as the project grows, since we can keep the same command to start our server in development if we choose to stop using nodemon, or if we want to add other process to start at the same time (like webpack or another CLI tool) using the same command.

Add nodemon to package.json as an NPM script
"scripts": {
    "start": "node ./app.js",
+   "dev": "nodemon ./app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

We now have an NPM script named dev for booting our server. Although we already have a script named start, we're leaving that alone to keep using node to start our server so that production servers that use npm start to boot the server won't use nodemon in production.

Start the Node server via NPM

First, make sure you don't already have an instance of your server running, then run:

npm run dev

We should see the same messages as before:

[nodemon] starting `node app.js`
Listening on port 3000

Which means our server has booted and we're ready to start coding!