DEV Community

Cover image for Let's build a simple REST API with Node.js and Express
Amrin
Amrin

Posted on • Originally published at coderamrin.hashnode.dev

Let's build a simple REST API with Node.js and Express

Hey there, hope you are doing well.

Today we will build a simple REST API with node.js and express.

Prerequisite:

  • Basic knowledge of Javascript
  • Basic understanding of command line
  • Installed node.js

What is REST API?

Let’s say you’re trying to find videos about Batman on Youtube. You open up Youtube, type “Batman” into a search field, hit enter, and you see a list of videos about Batman. A REST API works in a similar way. You search for something, and you get a list of results back from the service you’re requesting from.

An API is an application programming interface. It is a set of rules that allow programs to talk to each other. The developer creates the API on the server and allows the client to talk to it.

REST determines how the API looks like. It stands for “Representational State Transfer”. It is a set of rules that developers follow when they create their API. One of these rules states that you should be able to get a piece of data (called a resource) when you link to a specific URL.

Each URL is called a request while the data sent back to you is called a response.

This quote is from smashingmagazine, check out the complete article if you want to know more.

In a much simpler way, REST API is a way to communicate with the web server so that we can get the data, delete it and update it.

Enough definitions let's build the API.

Setting up the project:

  • create the project folder
  • run "npm init -y" to initialize the app
  • to install express run "npm install express"
  • create "server.js file" file

Open the server.js file in your code editor. Let's build the API:

const express = require("express");
const app = express();
const port = 5000; 

app.get("/", (req, res) => {
    res.send("hello world!");
});

app.listen(port, () => {
    console.log(`app listening at http://localhost:${port}`)
});

Enter fullscreen mode Exit fullscreen mode

Let's break down the code line by line:

On line 1 we exported the express from the node-module folder so that we can use it to create the server. In line 2 we initialized the server with the express() function. And in line three we created the port variable with the value of 5000.

In this line, we are initializing a route with app.get. What we are saying is, when someone goes to the root route (http://example.com/) then send a "Hello world" response.

at the end line, we are saying is run the server on PORT 5000.

Conclusion

That's it for today.
We've just built a simple REST API with node and express.
If you want a complete Node.js and Express project tutorial let me know in the comment below.
I'll write a detailed article if you are interested :)

And, I publish new articles every Tuesday and Friday.Follow me at @coderamrin me to get notified when I publish a new article.

You can also connect with me on Twitter at @coderamrin

Thanks for reading

Resources:

Discussion (2)

Collapse
alessandrogiuzio profile image
Alessandro

Thank you for the article!
Well done Amrin

Collapse
coderamrin profile image
Amrin Author

glad you liked it. and thanks for reading Alessandro :)