In this article, we are going to learn how to create Restful APIs  with Node.js and Express. We assume that till now you are having Node.js pre installed in your systems and you have are having basic knowledge of express,if not then head over to this articleto learn about these.

What is REST API?

REST or RESTful stands for REpresentational State Transfer. It is an architectural style as well as an approach for communications purpose that is often used in various web services development. In simpler terms, it is an application program interface (API) which makes use of the HTTP requests to GET, PUT, POST and DELETE the data over www.

Initializing A New App

Create a new folder wherever you want and open that folder in your favourite text editor and open the integrated terminal if available else open the default terminal and head over to the directory you just created.

Now type this command in your terminal which initializes your project.

$   npm install

This creates a package.json file in your folder.Now install the most used package in Node.js which is express (more about this is discussed here).To install this type the following command in your terminal and hit

$   npm install express –save

Then create the app.js file or whatever you prefer naming it (default is index.js) and add in the following code.

var express=require(“express”);
var app=express();
app.listen(3000,()=>{  console.log(“Server running on port 3000”); });

Congrats! You just made your first express server! So let’s head to the terminal and test this with this command.

$   node app.js

You will see this string in your terminal :- Server running on port 3000
If you see this then you are good to proceed else follow this article.

Creating Request Handlers

A server receives requests, processes them and returns a response. So you need to use routes to handle these requests. The requests have four major types, a GET request that gets data, a POST request that sends data securely, a PUT request that updates data and a DELETE request that deletes data.

Let’s create our first very simple GET request that returns a list of users. Under var app = express() in app.js, write down the following code.

app.get(“/names”,(req,res,next)=>{  res.json([“Mike”,”Sam”,”Retty”,”Tany”,”Lily”]);});

Full Code:

var express=require(“express”);
var app=express();
app.get(“/names”,(req,res,next)=>{ res.json([“Mike”,”Sam”,”Retty”,”Tany”,”Lily”]);});
app.listen(3000,()=>{  console.log(“Server running on port 3000”); });

This small function lets the express to use the url “/names” to return those names written inside res.json().

Now lets understand this line. The res is the response body and is used to handle response functions like .render() to render templates and .json() to return json data.

Run your application using the same command again and expect to see something like this on your browser.

Output


Now you are all set to make server side applications and link it to client side easily very easily.

That’s all I have and thanks a lot for reading. Please let me know if any corrections/suggestions. Please do share and comments if you like the post. Thanks in advance… 😉

Thanks Yash Kesharwani for helping us to grow day by day. He is expert in the backend services and loves to play around node.js.

Categories: Nodejs

0 Comments

Leave a Reply

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

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert