Creating REST API with Node, Express and MongoDB

Ish∆n
5 min readFeb 1, 2019

In this article we’ll go through the simple steps in making an Express web server, which is Fast and minimalist web framework for Node.js. We will also connect our express application with our database of choice Mongo DB in the cloud with mlab. I am using fish for my terminal, where I have installed fisher a fish plugin and switched my version of node to the latest version with fish-nvm plugin which is a Node.js Version Manager in the shell for fish. I will also be using Insomnia a REST client to test and check our endpoints.

Lets, now dive into setting up the project and writing some code. First lets initialize our node package manager with following command:

npm init -y

Now this should create a package.json file in our folder project Express_API. Now let’s create a src folder inside our project, inside where we will make our index.js file. We will also install express as our dependency and import it inside our index file. Lets also create a folder inside our project to serve a static file, and name it as index.html

../Express_API/public/index.html. Now we can add the file in our index.js. Our index file looks something like this now.

Lets add nodemon to monitor our file changes and restart the server for us, as our dev dependency.

npm i nodemon -D

Adding Routes

Lets now add some routes inside our express application, we will add a food route making a new folder inside our src file /src/routes/food.js which will look something like below with native supported ES6 syntax:

Now we will also add route parameter to our food route that can be mapped to a variable, these params property can be accessed on the request object. Lets see the following code:

If we check the endpoint in Insomnia adding a food name we will get back the response with the parameter. Now let’s also add a query string to our request object.

Now we can also pass in the additional query property to our request object like http://localhost:8080/food?foodName=pizza which if tested inside Insomnia would send us our query object as well. So, we now have two way to accomplish the same thing with query string and also with params.

Adding Middleware

Middleware are simply function that are executed in the request pipeline. It also has the ability to modify the request and response object and add additional details downstream.

Let’s setup test middleware inside our application, in our index.js file.

Our middleware function takes three parameter request response and next, which is the function for our next function in the pipeline. In the above code we have executed a middleware which is executed to foreach route. This is really powerful feature that we have which helps us to inject function to do anything to every route in the application.

Error Pages:

Now, let’s add some error screens to our application. We will add up 404 and 500 resource not found screens.

Here we added 404 to our application. If we check it inside Insomnia with the wrong URL it would now throw up the 404 screen for us. Now, lets also add up 500 error screen in our application as well. For this we will set up a separate file for the error screen inside our public folder. For accessing the static file inside our application we will require our path module in index.js. And we also add a new error route inside our food.js file.

Here we have required our path module to give path to our files and made a error handler for our screen 500 screen. Below we have the food route where we also added a new route to handle such requests.

Introducing Mongoose:

I logged into mLab account where I created a new database and named it as express-food. I have choose my cloud provider and lastly created a user where we will be using the MongoDB URI generated for connecting to our mongo database.

Example of generated key

We will get into focus on creating a CRUD API, we will define a model in mongoose and then we will perform CRUD operation with the Restful API we created. Now, we will add up models to our express application where we will connect and make CustomerSchema in path /src/models/customer.js

Now we will create a new file inside our route to perform our CRUD operation for the customers. In the mean time we need to install a new package to our application which is needed to parse the body object. So, lets install an additional package;

npm i body-parser

What this does is this will look at the incoming request if the content type of the request in the body type is JSON string it will create a property in the request object body and set the value there. Essentially it will parse out body for us. Now we will export out route and import it in our index file.

We have added our new CREATE request for customers with name and email. Now lets move on adding a new customer to our model.

Now let’s take a look at the UPDATE portion of our existing customers.

In the above code we have created a PUT request endpoint. After we test the end point it looks ok to move ahead with our last DELETE operation of CRUD of our customers. We have the following code now finally we have finished all of our CRUD operation and our endpoint looks ok after testing upon our tool of choice Insomnia.

Conclusion:

We have successfully connected our API with express and Mongo and made our CRUD endpoints to our Customer route. I think this article was useful to you. Thank you so much for your time and taking step for reading the article.

****** Happy Coding !! ******

--

--