How to create REST API in NodeJS?

Creation of REST API in NodeJS is very simple. For your information, REST is a type of web-service which is stateless communication over HTTP. API means that provide a interface to communicate between same or separates systems. Here are basic information about REST API.

Let’s start work to create REST API

  1. Create folder as “MyServer” on your local drive.(i.e D:\MyServer).
  2. Download and Install NodeJS on your system.
  3. Open folder in Visual Studio Code (you may use any IDE).
  4. Press Ctr+` to open terminal tab in VS Code.
  5. Type node –version to check NodeJs version.
  • Type npm init to create package.json file for MyServer project.
  • Press Enter key until you will see message ‘ Is this OK? (yes) ‘
  • Now you can see file is created in MyServer folder.
  • Content of package.json should be as following:
  • Now run this command npm install express –save

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
  • Create new JavaScript file index.js in Myserver folder. Put the following code in this file.
  • Type node index.js on your terminal. Code in index.js will be executed or served by NodeJS.
  • Open browser and type http://localhost:3001/
  • Hello World! should be display on your browser. That’s mean your code is working! Your code is running on port 3001.
  • Create folders and JavaScript files ( just for better code management ) as following:
  • Now press Ctrl + C to stop your application/server.
  • Create and export model LoginModel by adding following code in app/models/LoginModel.js

Login class contain getLogin() method. This method has two parameters: req will use to receive data from controller and 2nd parameter result is a callback function. This callback function will return two parameters. First parameter will use for error message and second parameter will use for data.

  • Create and export controller LoginController by adding following code in app/controllers/LoginController

Since this API getLogin call by POST method that’s why you will use req.body. Here res will use as HTTP response. Here res.status(500) means Server error and res.status(200) means OK. This status will send to client along with result or error message.

  • Create and export route LoginRoute by adding following code in app\routes\LoginRoute.js
  • Add LoginRoute in your index.js

Please note that without these lines you are not able to use req.body

Here ‘/auth’ is your API path. -> app.use(‘/auth’,loginRoutes);

  • Now start your server on port 3001
  • Call your new created REST API by Postman to check either your API is working or not…
  • Insert wrong password and click on send button.
  • To ensure your server always keep online and automatically restart after your server/machine start. Please follow these steps:
    • Install and setup PM2. -> npm install pm2 -g
    • pm2 start index.js
    • pm2 startup systemd
  • Open your browser and type URL http://localhost:3001 . If Hello World! is displaying… that’s mean everything is code. Now you are able to use your REST API http://localhost:3001/auth/login

Here is a topic related to SOAP API for you!