Simple Node.js RESTful Web API

Guys, last time we were talking about node.js & jenkins. Today, i got something more techie which we called as REST architecture. If you are into modern day web application development, this is not new for you. What is REST?
Simply we can say that REST is web standards architecture & uses HTTP protocol. RESTful web service implies that it uses REST architecture to implement it’s services. Simply the CRUD operation that we are talking. Let’s get into work. 😊

I assume that you guys have already installed node.js in your development environment. so let’s create our basic node.js RESTful api. Before moving into that let’s create our simple project within few minutes. Hope you guys have some basic javascript knowledge.

Let’s first create our project folder in local file system. (For demonstration purpose, i’m using the folder called restapi.)
Navigate to the folder using your terminal.

Let’s command npm (Node Package Manager) to create a basic Node.js Project. Actually what this command does is creating the package.json file for us.😇😇

after creating package.json we are ready to install express framework. express will make the things easy.

This will install express withing node_modules folder.
Then we need to install following three packages along with the express.

Body parser is a node middleware which helps to handle form data.
multer is another middleware which handles multipart/form data.
Cookie-parser will parse cookie header

Now the initial works are done.
Let’s get back to the RESTful API implementation for customer ordering system.

Imagine, that we have to implement a RESTful web services to

List down all the customer orders in Database
Add new Customer Order

Delete particular Order
app.get(‘/listOrders’, function (req, res) {

// body of the express application get function

** if you have good Javascript knowledge this is nothing for you to understand.😜😜 Get Details of a particular customer order

Here i’m not going to use any Database for the Demo. Let’s create simple JSON file here as our DB. 😆

Let me show you the json file that i have created.(orders.json)

This acts as our demo DB.

First RESTful API is /listOrders

Conceptual wise only you need to understand is express application uses callback function whose parameters are the Request & Response objects.

Below is the code that i implemented to list down all the orders in our DB. (actually now it is orders.json file)

I’ll explain the code little since it is the first RESTful API that we’ve created.
* first it imports the express module and assign it to JS variable.
* Then we assign the express() function call to app variable for further uses.
* Also we are importing the module fs for file accessing. because here we are reading the file orders.json.
* Then the express get function call. /listOrders is one parameter for the function which is actually the endpoint route. also it takes another parameter which is a function who takes express request and response objects.

Below is the output. (access the url localhost:3000/listOrders)

Cool. 💪💪💪 Here we got all our customer orders as a json object.

Let me implement the /addOrder RESTful API now.

Very easy. 😀

Check the below code care fully.

Only thing changed is that
This is not a get method. Here we are adding new order to the DB. So this should be a HTTP POST method. (hope you have basic HTTP understanding 😛😛) i have commented the code, so beginner can understand the code. now let’s get the results.
Let’s use postman to make this request to the Node server. Because this is a POST request

Just Don’ type this in your chrome browser, this is not a get request 😋😋
See the result in postman.

Look, we already have that “MyNewlyAddedOrder” in the JSON String. 💪💪💪💪💪

We are done with 2 RESTful APIs for our new customer ordering system. Your task is to implement next two.

Delete an Order from the DB
Get details of an Order
Hint: Deletion is a HTTP DELETE Request , Get Details of particular order is a HTTP GET Request. 😊😊

Hope you can create your own Node.js RESTful api for your application. There are many things to know when it comes to real world applcations. But this is the Base for all those applications. Understanding those things, make you easy to deal with Enterprise level applications. 💪💪💪💪

Originally published at on May 26, 2018.

Senior Software Engineer - Technology Team | IFS R&D International (PVT) Ltd |Graduated from University of Moratuwa in Computer Engineering field