Creating a simple REST API using NodeJS and ExpressJS

The REST APIs are basically a URL endpoint that is generally used by a client-facing application that communicates with your servers using the same mentioned URL. REST API can be used for authentication, data fetch, data update, etc.. It can also be used by third-party services that need some data from your servers, you can easily create an API and exposed to them without revealing your internal system.

In this article, we will see how we can create such APIs using NodeJS. We will create a simple REST API without using Database just for the sake of keeping it simple. I’ll add another post where we will be doing some CRUD operations using Database. Make sure you’ve NodeJS installed on your machine, if you don’t, you can follow this Link to do the same. So let’s get started.

First, create a directory of choice, inside that directory open up your terminal or cmd and install ExpressJS, type following

npm i express

now let’s create a entry point for our application, create a file named index.js and add the following code,

// importing express.js
const express = require('express');
// initialising our express app 
const app = express();
// defining port where server will run, you can define the port you want to use, I'll be using 8000
const port = 8000;

// creating our first API endpoint
app.get('/', (req, res) => res.send('Hello World!'));

// this will start the server and will listen on our defined port
app.listen(port, () => console.log(`app started on http://localhost:${port}`));

Now to start the server, type following on your terminal or cmd,

node index.js

you’ll see app started on http://localhost:8000on your terminal. To verify open http://localhost:8000 on the browser you’ll see Hello World on screen. You’ve created your first REST API in NodeJS.

Let’s create an API that will return some fake user data in JSON format since we’re not using the database we will be using faker.js for data. Let’s stop the server and install this library after that do some changes to our index.js

npm i faker
// importing express.js
const express = require('express');
const faker = require('faker');
// initialising our express app
const app = express();
// defining port where server will run, you can define the port you want to use, I'll be using 8000
const port = 8000;

// creating our first API endpoint
// res.send is use to send HTTP response
app.get('/', (req, res) => res.send('Hello World!'));

//
app.get('/user', (req, res) => {
  // creating user object
  const user = {
    randomName: faker.name.findName(),
    randomEmail: faker.internet.email(),
  };
  // now sending json response
  // res.json is use to send json response
  res.json(user);
});

// this will start the server and will listen on our defined port
app.listen(port, () => console.log(`app started on http://localhost:${port}`));

now start the server again and go to http://localhost:8000/user, you’ll see some random name and email. We’ve created another API.

I’ll add a post soon for creating a CRUD REST API using Node, Express, and MySQL, in that we will see API creation in little more detail.

Leave a Reply

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