Create REST API in Node.js

Today we will show you how to create REST API in Node.js. So we can use it in front-end application like ReactJS, Angular, Vue.js, etc.

Create REST API in Node.js, Building a simple REST API with NodeJS and Express, Creating a Secure REST API in Node.js. Building a Restful CRUD API with Node.js, Node.js and Express Tutorial: Building and Securing RESTful API, Example/Demo of Node.js REST API.

Steps to Create REST API in Node.js

  1. Setup environment
  2. Initialize the project
  3. Setup the server using express
  4. Create server and add routing HTTP request
  5. Output

1. Setup Environment

In order to create REST API, We need Node.js is installed in system. Run below command to check the Node.js is installed or not. You can also use this command to check the version of it.

If you don’t have it installed then use below link to download & install Node.js in your system.

2. Initialize the Project

I presume that you already have your environment set up. Kindly follow the below steps to initialize the project.

Now let’s create project directory name as “first-node-app

Jump into the newly created directory via terminal.

Create a package.json file via below command in terminal.

package.json is a file that gives the necessary information like project name, version, description, project’s dependencies, etc.

npm init” will prompt you to enter some information such as name, version, description, entry point, test command, git repository, keywords, author, license, etc. At last type yes and press enter to complete the creation process.

npm init - Create REST API in Node.js - Clue Mediator
npm init – Create REST API in Node.js – Clue Mediator

If you want to skip all these things and directly create a project then just run below command.

After completing all these things, you will see the package.json has been created in your project directory.

3. Setup the Server using express

To set up the server, We need express framework for Node.js. Run following command to install express in project.

Next, We will install nodemon package to track the file changes. nodemon is a tool that helps to auto restart the application when any file changes detected in the project directory. Run following command to install nodemon.

The “--save-dev” in above command will helps to install package as development dependency.

On successful installation, your package.json file will be modified to have the two newly installed packages.

4. Create server and add routing HTTP request

Let’s create “server.js” file at root level and type/copy following code.


In order to serve the application through nodemon, You have to add below command in “scripts” attributes of package.json file.

Now your updated package.json file should look like below.

package.json - Create REST API in Node.js - Clue Mediator
package.json – Create REST API in Node.js – Clue Mediator

By executing below command, you can start the application server. It will serve the application on 4000 port (http://localhost:4000).

You can’t see anything in browser on http://localhost:4000 link because we are not serving any data on root URL.

So let’s add the request handler to serve the data on root URL. Add below code in server.js file to make it work.

Your whole server.js file should look like below.

That’s it!! Now check the URL (http://localhost:4000) again and it will serve you the above mentioned text in browser.

Because of nodemon you don’t have to restart the application. It will auto restart the application once file changes are detected.

5. Output

Output - Create REST API in Node.js - Clue Mediator
Output – Create REST API in Node.js – Clue Mediator

Demo & Source Code

Github Repository Postman Collection
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

3 Responses

  1. mohammed says:

    aaah am a new in this programing language i almost used php nw i wanna move on this react but each site gives me different ways that confucing me where to beggin

    • Clue Mediator says:

      Yes, It’s very difficult to find the correct way to learn ReactJS. I would recommend you to start learning all the react article from ReactJS category and it would be good if you will start reading from oldest article.

      Feel free to share your queries with us.
      We are happy to help you!

      Join us on social pages & Share with your friends. Happy Coding..!!

  2. Clue Mediator says:

    Hey Mohammed,
    We have created the separate category ReactJS Tutorial where we are listing the react articles. You can read it step by step.

    Let us know if you are finding any difficulties.

    Clue Mediator

Leave a Reply

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