RESTful API with Express and NodeJS

Create RESTful API in NodeJS Easily and Quickly

Parag Mahale
Towards Dev

--

create restful api in nodejs and express
RESTful API with NodeJS

Nodejs is a JavaScript runtime that runs on chrome’s V8 engine and executes JavaScript programs outside web browsers.

Nodejs is very powerful as it allows javaScript to be used as a server-side language, which is achieved by the modules/packages it comes with, such as httpwhich allow it to create an HTTP server.

With nodeJS, we can also build GUIs, Mobile applications, Desktop applications, APIs, Command Line Tools, etc.

What is a RESTful API

REST (Representational State Transfer) is an architectural style that defines a set of constraints to be used to create web services. REST API is a way of accessing web services in a single and flexible way without having any processing.

How REST API Works

A request is sent by the user to the server in the form of a web URL as a GET, PUT, POST or DELETE request. After that, a response comes back from the server in the form of HTML, XML, Image, JSON, etc.

Client sending request, and receiving a response from the server
Client sending request, and receiving a response from the server

HTTP Methods

  1. GET method: It indicates to get/retrieve the resources requested.
    Ex. a GET method for students database will give you the list of all students.
    In GET, if we want to send some additional data to the server, it is done via the URL, i.e. the data will be embedded into the URL.
  2. POST method: It’s used to add a new resource.
    Ex. a POST method to students database will create a new student with given attributes.
    In POST, additional information is not reflected in the URL.
  3. PUT method: It is used for updating resources, but it can also be sued to create a new resource if the resource ID is provided by the user.
    Ex. If student ID 05 does not exist, PUT will add a new student.
    PUT is not a safe operation but it is idempotent.
  4. PATCH method: It is used for updating resources. In PATCH, the request only needs to contain the changes, not the complete resource.
    Ex. a PATCH request for student ID 01 with only its changed address will modify its address.
  5. DELETE method: It is used for deleting resources.
    Ex. DELETE request for ID 07 will delete the resource with ID 07.

RESTful API with Express and Nodejs

Make sure you have node and npm installed

Open the terminal and execute the following commands

Create a folder named node-rest-api

mkdir node-rest-api

Navigate into the folder

cd node-rest-apimkdir info

Initialize npm with its default values

npm init -y

Create index.js and crud.js files

touch index.js crud.js

Install express

npm i express

Open the folder in the text editor of your choice.

First, we’ll add the following line to the package.json file.

package.json
package.json

First, let’s create an express server that will listen on port 3000.

In index.js,

index.js
Index.js

Run the command npm run start to start the server.

server started!!
server started!!

Now, the server is live on port 3000

NOTE: We’ll have to restart the server every time we make changes.

Add the following lines to index.js.

index.js
index.js

Middlewares are pieces of code that run before any routing process is executed.

Here, we are using the express.json() and express.urlencoded() middlewares to enable us to send a response in JSON format with res.json() and parse the URL body and extract data out of it, especially for POST method data that doesn’t appear in URL in req.body, respectively.

Let’s add crud to the application.

In crud.js,

crud.js
crud.js

In express, Router() is used to easily add routes.

For Adding a file,

POST method in crud.js
crud.js — POST method

Here, we are creating a JSON file with id and other information provided in req.body.

POST method response
POST method response

For reading a file,

GET method in crud.js
crud.js — GET method

Here, we are reading a file with the ID provided in the URL.

GET method response
GET method response

For updating a file,

PATCH method in crud.js
crud.js — PATCH method

Here, we are re-writing a file with ID with the data provided in req.body.

patch method response
PATCH method response

For deleting a file,

DELETE method in crud.js
crud.js — DELETE method

Here, we are deleting a file with the ID provided in the URL.

DELETE method response
DELETE method response

Thus we have created a RESTful API that can create, read, update and delete JSON files.

Full code for this blog is available on Github.

Link to my other articles:

--

--