background
Home / Articles / Build API in less than 2 minutes
ARTICLE

Build API in less than 2 minutes

Building a simple REST API in less than 2 minutes with Node.js, and Express.js.

#API  #NodeJS  #ExpressJS  
  Rafi Putra RamadhanNovember 25, 20225 min read  

Build API in less than 2 minutes

In this article, we will build a simple REST API in less than 2 minutes with Node.js, and Express.js. ✨

So, let's get started! 🚀

Getting Started

Download Node.js

We need to setup a Node.js server. For this purpose, download and install it if you haven't already. You can download it from here. I recommend you to download the LTS version.

Setup project

Create a new directory for your project. I will name it simple-api.

1mkdir simple-api
bash

Open the directory with your favorite code editor. I will use Web Storm.

After that, we need to initialize the project first. Run the following command in your terminal to create a package.json file.

1npm init -y
bash

Adding scripts

Open the package.json file and add the following scripts.

package.json
1"scripts": { 2 "start": "node index.js" 3}
json

Don't worry about the index.js file for now. We will create it later. For now, we will install the dependencies.

Install Express.js package

We need to install the Express.js package. Run the following command in your terminal to install Express.js in the project.

1npm install express
bash

And if you look at the package.json file, you will see that the Express.js package has been added to the dependencies section. 🎉

package.json
1"dependencies": { 2 "express": "^4.18.1" 3}
json

Note: Don't worry if we have different versions of the Express.js package. It's okay. 😊

That's mean we already have the Express.js package in our project. 🎉

Setup the server and create the first route

Create a new file named index.js in the root directory. Open the file and add the following code.

index.js
1// importing packages 2const express = require('express'); 3const app = express(); 4 5// defining port 6const port = 5000; 7 8// adding a routes 9app.get('/', (req, res) => { 10 return res.status(200).json({ 11 code: 200, 12 status: 'OK', 13 message: 'Our API is working!', 14 data: [ 15 { 16 id: 1, 17 superhero: 'Thor', 18 }, 19 { 20 id: 2, 21 superhero: 'Ironman', 22 }, 23 ] 24 }); 25}); 26 27// starting the server 28app.listen(port,() => console.log(`Server is running on port: ${port}`));
javascript

Code Explanation

  • First, we have imported Express.js into our index.js file.
  • We have created an Express.js application by calling the express() function.
  • We have defined the port number. In this case, we will use port 5000.
  • We have added a route. In this case, we will use the GET method. We have also added a callback function that will be executed when the route is accessed.
  • We have started the server by calling the listen() function.

Run the server

Run the following command in your terminal to start the server.

1npm start
bash

If you see the following message in your terminal, it means that the server is running.

1Server is running on port: 5000
bash

Test the API

You can directly access the API by opening the browser and go to the following URL.

Or you can use HTTP client like Postman or Insomnia to test the API.

Make a GET request to the endpoint http://localhost:5000/. You will see the following response.

1{ 2 "code": 200, 3 "status": "OK", 4 "message": "Our API is working!", 5 "data": [ 6 { 7 "id": 1, 8 "superhero": "Thor" 9 }, 10 { 11 "id": 2, 12 "superhero": "Ironman" 13 } 14 ] 15}
json

Congratulations! 🎉 👏 You have successfully built your first REST API in less than 2 minutes. 🎉 🎉 🎉 👏

Conclusion

In this article, we have built a simple REST API in less than 2 minutes with Node.js, and Express.js. ✨

We have learned how to setup a Node.js server, install Express.js package, and create the first route. 🎉

I hope you enjoyed this article. If you have any questions, feel free to ask me. 🙏

References