Restful apis in Node Js with Mongo DB | Developing web services in Node Js & Mongo DB

In this tutorial I'm going to explain how to develop restful apis node jsI will be using MongoDB as database. I will be explaining simple apis which can add,fetch all, fetch by id, delete or destroy and update the records.

To start with we need to install mongoose by following command if we have not installed it yet.

To install the mongoose:                                                                                                                        

Here is the post route which contains all functions add,fetch all, fetch by id, delete or destroy and update the records.

routes/post.js

MongoDB connection using mongoose with node JS:

Here I am creating connection with MongoDB by using mongoose.
Write the following code in app.js
Add route in app.js also:
Create a new route file in main directory:
routes.js
Create schema for blog post:
schema/post.js
 Here I'm defining schema for posts in MongoDB or in terms of MongoDB I'm defining collection here.
Response :
common/response.js
 This file is used to return the json response in a format. Here I have created two functions errorResponse is to return the error response and successResponse is to return the success response in json format.
 You can download the web services node js code from here.

Share This:

Leave a Reply

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