RESTful API with Express and NodeJS
Create RESTful API in NodeJS Easily and Quickly
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 http
which 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.
HTTP Methods
- 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. - 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. - 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. - 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. - 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.
First, let’s create an express server that will listen on port 3000.
In index.js,
Run the command npm run start
to start the server.
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.
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,
In express, Router()
is used to easily add routes.
For Adding a file,
Here, we are creating a JSON file with id and other information provided in req.body
.
For reading a file,
Here, we are reading a file with the ID provided in the URL.
For updating a file,
Here, we are re-writing a file with ID with the data provided in req.body
.
For deleting a file,
Here, we are deleting a file with the ID provided in the URL.
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: