Clue Mediator

Create REST API in Node.js

📅November 24, 2019
🗁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.

Checkout more articles on React JS:

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.

node -v OR node --version

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

https://nodejs.org

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.

npm init

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.

npm init -y

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.

npm install express

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.

npm install nodemon --save-dev

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.

server.js

var express = require('express'),
    app = express(),
    port = process.env.PORT || 4000;

app.listen(port, () => {
    console.log('Server started on: ' + port);
});

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

"serve": "nodemon server.js"

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`).

npm run serve

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.

// request handlers
app.get('/', (req, res) => {
    res.send('Welcome to the first Node.js Tutorial! - Clue Mediator');
});

Your whole server.js file should look like below.

var express = require('express'),
    app = express(),
    port = process.env.PORT || 4000;

// request handlers
app.get('/', (req, res) => {
    res.send('Welcome to the first Node.js Tutorial! - Clue Mediator');
});

app.listen(port, () => {
    console.log('Server started on: ' + port);
});

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