Create REST API using Node.js, mongoose and restify

In this tutorial, we will learn how to create Restful API using Node.Js, mongoose and restify.

Prerequisites

  • Node.js
  • MongoDB

Before creating Restful API using Node.js, we need to install Node.js and MongoDB in our system.We can download and install these software by following link :-

Node.Js :- Install Node.Js
MongoDB :- Install MongoDB
Once you installed software in your system, you can check its installed or not by following command :-
Check Node.Js installation

Check MongoDB installation

Setting Up Our Application

Now we will create project folder and we will do setup for node.js basic application.

Now we will create package.json file and we will define all the dependency of our node application.

package.json

Here we are going to use restify and mongoose package so we defined in dependencies.
Now we will run the following command to install all packages through npm.

After running this command node_modules folder is created in out project directory.we can check all installed model by running this command in terminal also :-

config.json [Define All configuration here]

We will define all configuration of project in config.json file.

Here dbPath is the path of mongoDB for connection and rest-api is our database name.
port is used for running the Node.js application on this particular port number.We can change these setup anytime according to our requirement.

Application Setup [app.js]

here we define the application setup of restify application, which will run on port number 8000 as we defined in config.json file.Before running this application we will define the routes.js file, where we will create all our routes for this application.

routes.js

Now we will run this command to run our application and we will see in the browser or terminal to see the output.Currently we have only one route i.e. ‘/’ which will point to home page and display WELCOME TO REST API” on this url :- http://localhost:8000 in browser.

Output in Terminal

restify_example



db.js [define the mongoose connection]

Here we will define all the mongoose connection code.

Now we will define schema for our collection.For this first we will create models
folder inside our project and in that folder we will define our all schema.

Student Schema [models/studentSchema.js]

Now we have created the schema for student collection.Now we will define our business logic in controller folder.We will create controllers folder inside project folder.

Controllers [controllers/studentController.js]

In this file we will define all business logic for our REST API like Create Student in database, get all student details from database etc.

In this controller we have two function :-

  1. createStudent :- This function is used to create new student and save data in collection and this function is called by REST API url :- http://localhost:8000/createStudent with POST method and its routes is define in routes.js file.
  2. getStudent :- This function is used to get all student details from collection and this function is called by REST API url :- http://localhost:8000/getStudent with GET method and its routes is define in routes.js file.

Now we need to update routes.js file to access these function.

routes.js

Now our REST API for student application is ready.Now i will show you the running application and how we can call these API using postman.Before this i will show you the complete folder structure of the project.
project_structure

Check the API Using POSTMAN

Now we will run our application using the command

we can check the running restify app by clicking on browser.URL :- http://localhost:8000/ Output will show in browser is :-
running-app

createStudent API in POSTMAN

createStudent

getStudent API in POSTMAN

getStudent

Conclusion

In this article, we learned about how to create Restful API using restify and mongooose. In the process, we also learned how to create schema using mongoose and how we can test our REST API using POSTMAN.Please give your valuable feedback regarding this article and please post comment if you any queries regarding this article.

About Ritesh Kumar

I am Full Stack Developer and Blogger from Bangalore, India.

3 thoughts on “Create REST API using Node.js, mongoose and restify

  1. Help !! How delete method add in project?
    //routes.js
    app.del(‘/delete/:id’, student.deleteStudent);
    //studentControlles.js
    this.deleteStudent = function (req, res, next) {
    Student.remove({id: req.params.id }, function (err, result) {
    if (err) {
    return res.send(err.message);
    }
    return res.send(result);
    });
    };

    I add code my project is error

  2. I did all of these but I could not connect to server, when I write node app.js in terminal, I received message server listening port on 8000 but also I received “error occured from db ” message . I could not find the solution help please 🙁 also I defined the path of Mongodb

    1. Please check your Mongo DB server is started or not. You need to start your Mongo DB before running this application.

Leave a Reply

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