November 25, 2022
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!
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
.
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.
Adding scripts
Open the package.json
file and add the following scripts.
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.
And if you look at the package.json
file, you will see that the Express.js package has been added to the dependencies
section. π
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.
β¨ 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.
If you see the following message in your terminal, it means that the server is running.
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.
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