What is mongoDB and how to get started.

In this article I will share how to build Restful API with Nodejs Express MongoDB

In order to develop and run this restful API, we need Node.js, which is JavaScript runtime installed on our machine. With Node.js comes NPM which is a node package manager that we will use to install other packages like Express needed to develop this API.
Also, we need to have MongoDB installed on the machine.

MongoDB Installation/Setup
MongoDb is a no-SQL database that stores data in the form of JSON object which is called Document. All Documents are stored in the container called Collection. So first Collection is created and then Document in inserted into it.

Get Started…
Start with creating project folder.

$ mkdir webapi

$ cd webapi

Now we will start with creating package.json file which is the first building block for Node.js application and holds application configuration and package dependencies details that is required in order to run the application.

$ npm init --yes

Install Express, Mongoose and body-parser

Mongoose is a MondoDB object data modelling package that provides a straight-forward, schema-based solution to model data in the Node.js application.
body-paser is Node.js middleware for parsing incoming request bodies in a middleware before request handlers and is available under the req.body property.

So install all these packages by running following command in the command window.

$ npm i express mongoose body-parser –save

–save option will add entry for these packages in the dependencies section in package.json file for our project.

Write Code…
Now open the code editor to start writing code.

Create db.js file and add following code to create and export connection with MongoDB using mongoose.