Make your own Rest API in NodeJS.

This article will guide you on how to make a rest API in NodeJS using Express.
Prerequisites:
1. Basic knowledge about javascript
2. Basic knowledge about NodeJS

What is a REST API/ What is Express?

Overview
A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding.

What is REST?
REST is a set of architectural constraints, not a protocol or a standard. API developers can implement REST in a variety of ways.

What is an API?
An API is a set of definitions and protocols for building and integrating application software. It’s sometimes referred to as a contract between an information provider and an information user — establishing the content required from the consumer (the call) and the content required by the producer (the response). For example, the API design for a weather service could specify that the user supplies a zip code and that the producer reply with a 2-part answer, the first being the high temperature, and the second being low.

What is Express?
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications

Building the API:

The steps involved in building a REST API are written below. Make sure to follow these to follow them in order —

1.Installing NodeJS and npm
You have to install NodeJS on your PC/WorkPlace. You can download NodeJS from https://nodejs.org/en/download/.

2. Initiating a project
For initiating a project, first, you have to open your terminal/console and create a new folder using on desktop usingcd desktop && mkdir my_api && cd my_api
Now run npm init -y command in the same folder, this should create a file named “package.json”. Now open this folder in your favorite IDE (Visual Studio Code, Notepad, etc) and create a file called index.js .

3. Installing and setting up Express
Once everything is set up, run npm i express command in your terminal in the same folder. This should install the express npm module.
Now let's open index.js and add start coding!

Import the module —
import express from ‘express’ or const express = require(“express”)

Construct a new app —
const app = express()

Decide which port you want to run the API on —
const port =6969

Make the API listen to that port —
app.listen(port, () => {
console.log(`Example app listening at
http://localhost:${port}`)
})

Final code till now —

Now, run node. and if you try to visit the URL http://localhost:6969 you should see something like this -

If you see something like this, then, congratulations! Your API is now working and all we need to do is configure endpoints

4. Configuring endpoints
Configuring endpoint(s) is really simple. All you need to do is add this bit of code —
app.get(‘/’, (req, res) => {res.send(‘Hello World!’)})
Now if you will visit localhost:6969 again, you should see something like this

What is req?
Short for request, the req object is one half of the request and response cycle to examine calls from the client-side, make HTTP requests, and handle incoming data whether in a string or JSON object

What is res?
The Response object (res) specifies the HTTP response which is sent by an Express app when it gets an HTTP request.

What can I do with req and res?
You can do a lot of things with these 2 objects, you can get the headers/set the headers, get path parameters, get query parameters, and much more! I recommend checking out the following links for more information —

Express Documentation
Express Examples

5. Final code
The final compiled code is —

Conclusion

So there you have it, your very own rest API made in NodeJS using Express. There are a lot of things you can do with the express module and I suggest checking out their official website for more details
Good luck with your journey ❤

My Discord server
My email address
Donate me

A self-learning Developer who is always eager to make more projects/articles to help other users. You will find articles related to different languages here.