Make your own REST API with MongoDB with Mongoose, Express.js, and Node.js

Node.js, Express.js, Mongoose.js and MongoDB is that the nice combination for building simple and quick REST API. you may see how fast that combination than other existing frameworks because of Node.js could be a prepacked compilation of Google’s V8 JavaScript engine and it works on non-blocking and event-driven I/O. Express.js is javascript net server that has complete function of web development including REST API.Before we begin, confirm you’ve got put in all tools that required.

this tutorial, we’ll work using Terminal and your favorite code editor as the text editor. For Windows user, you’ll be able to work exploitation Node.js command. we have a tendency to started this Node.js, Express.js, Mongoose.js and MongoDB tutorial from scratch and then continue a quicker method using Node-Restful library. Let’s begin.

Step 1. Create Express.js Project along w/d Required Modules

Start your CLI terminal or node command line the go to your projects folder. First, install express generator using this command.

Next, create an Express.js app using this command.

This will create Express.js project with the EJS view instead of Jade view template because using ‘-e’ parameter. Next, go to newly created project folder then install node modules.

To run the server

Shows like this in your the terminal.

Start your browser at ‘localhost:3000’, if you see like the image below then your server is ready.

Step 2. Add Mongoose.js Module as ORM for MongoDB

Stop node by push CTRL+C on the keyboard. Type this command to install Mongoose.js module.

Before running your server again, make sure MongoDB server is running. Open another terminal, then type this command to start MongoDB server.

Now, open and edit app.js in the root of project folder using your favorite text editor or IDE. Declare mongoose in require sections.

Use mongoose with native Node Promise.

Create a connection to MongoDB.

Next, run the node app again.

You should see this message on the terminal. It’s mean connection to MongoDB is successful.

Step 3. Create Product Mongoose Model

Create models directory and javascript file as the model. stop node server by pushing CTRL+C key in the terminal.

Open and edit Product.js file the add this lines of codes.

That Schema will mapping to MongoDB collections called product. If you want to know more about Mongoose Schema Datatypes you can find it here.

Step 4. Create Routes for REST API endpoint

The REST API that will have following functions.

Method Endpoints Notes
GET  /product Get all products
GET  /product/:id Get single product
POST  /product Add product
PUT  /product/:id Update product
DELETE  /product/:id Delete product

Add the javascript file to routes folder.

Edit routes/products.js then add this lines of codes.

Edit app.js then add product route as require after users require.

Add use after use of users.

Step 5. Test REST API Endpoints

After everything is ready, this time to testing our created Node.js, Express.js, Mongoose.js and MongoDB REST API. There are so many tools for testing REST API, but for now, we are testing using CURL from the terminal.

We start with Add/Save product data first. Open new terminal tab or windows the type this command.

If you get the response like below, then you save new product successfully.

We can create the same POST with different data to populate more record to product collection.

Next, we are testing to get all products data using this command.

That command will response products data with JSON format.

Next, we are testing to get one product by id using this command.

The response should be like this.

Next, we are editing and update one of a product by id using this command. First, copy id from one of product from the response before then paste it as the parameter.

It should response like this.

Finally, we are testing for delete one product by id using this command.

It will response like this and product with that id will be removed from product collection.

Step 6. Quick REST API Creation using Node-Restful Library

There are so many method and library for creating REST API quicker and elegant. One of them that I found is the Node-Restful library that you can find on Github. Just add the library using npm command.

Open and edit app.js on the root of the project. Add require line for Node-Restful and Method-Override.

Change “bodyParse extended” value to true then add this lines.

Create mongoose model that use together with Node-Restful library. Right now we are creating “category” model.

Run again the app and test it using previous way for testing REST API.

Leave a Reply

Your email address will not be published. Required fields are marked *