Building a Restful CRUD API with Node.js, Express and MongoDB

November 25, 2018
Restful CRUD API with Node.js, Express and MongoDB

In this tutorial, we are talking about HOW TO create a Rest API using Express js Node js with MongoDB to make a CRUD.Express.js is a framework of Node.js.It is a very lightweight framework and also easy to use.


MongoDB-: MongoDB has become one of the maximum popular NoSQL databases, being used because of the backend for plenty of important websites consisting of eBay, Craigslist, SourceForge, and The big apple times. MongoDB is available below the GNU Affero preferred Public License whilst its language drivers are to be had below the Apache License. There are also industrial licenses being supplied.


Step-1: Firstly we are going to create directory diff then we install the application.
                $ mkdir node-mongo-crud
                // now go to the directory
                 cd node-mongo-crud

Step-2: After installing the application then we are going to create a package.json file using this command.

                                                     $ npm init 

Step-3: After creating the file we are going to install dependencies using the npm command if you are run the npm init command then ask the following things.

  
Is this OK?  yes

see the application called server.js
Step-4: we are installing a package then install the dependencies using the command:
        
          $ npm install express body-parser mongoose --save
        --save option is used to save all the dependencies in the package.json file.

Step-5: we are making an entry pointing to this application using to touch Linux command.
                                  $ touch server.js
                                                   directory structure :
                                                   node-easy-notes-app
                                                  └── node_modules/
                                                   └── package.json
                                                    └── server.js
Step-6: Weare creating a database configuration and also make a directory "config" inside the root directory of the application config file.

     

Step-7: In this step, we are creating a Model schema then create a new directory "app" inside the root folder of the application we are creating another directory of the app folder by the command.
     

   
Step-8: We are Add the Routes the create a "routes" directory and also create a route file "category.route.js" in the routes directory.
       
Step-9: We are adding routes in server.js then the project wants the server for REST API.
   
Step-10:  We are going to add a controller then create a controller directory with the app and also controller file "category.controller.js" using by directory command.