ArticleAPI4 min read

Build API in less than 2 minutes

In this guide, you'll learn to build a simple REST API in under two minutes using Node.js and Express.js. Follow the steps to set up your server, create routes, and test your API effortlessly!

raprmdn
Rafi Putra Ramadhan
API
NodeJS
ExpressJS
Build API in less than 2 minutes

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.

Terminal
mkdir simple-api

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.

Terminal
npm init -y

Adding scripts

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

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

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.

Terminal
npm install express

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
"dependencies": {
  "express": "^4.18.1"
}

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
// importing packages
const express = require('express');
const app = express();
 
// defining port
const port = 5000;
 
// adding a routes
app.get('/', (req, res) => {
  return res.status(200).json({
    code: 200,
    status: 'OK',
    message: 'Our API is working!',
    data: [
        {
            id: 1,
            superhero: 'Thor',
        },
        {
            id: 2,
            superhero: 'Ironman',
        },
    ]
  });
});
 
// starting the server
app.listen(port,() => console.log(`Server is running on port: ${port}`));

✨ 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.

Terminal
npm start

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

Terminal
Server is running on port: 5000

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.

json
{
  "code": 200,
  "status": "OK",
  "message": "Our API is working!",
  "data": [
    {
      "id": 1,
      "superhero": "Thor"
    },
    {
      "id": 2,
      "superhero": "Ironman"
    }
  ]
}

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

  • Node.js
  • Express.js
  • Postman
  • Insomnia