In this tutorial, you will learn how to create a small REST API using the Express library for Node.js. Express is one of the most popular libraries for API creation. It is also available in some major Cloud functions providers like Firebase.
Before continuing, you can learn more about REST APIs in general, how they work, their components, etc. in our series of tutorials
Creating an API using this library is easy, straightforward, and only requires the following basic steps.
To create a simple API using Express, we first need to initialize a new NPM application.
To do so, first verify that Node.js and NPM are installed by running the following commands:
Go to Node.js' website to install Node.js and NPM if it is not already done.
After verifying that Node.js and NPM are installed, you can initialize a new NPM application by running the
npm init command in a new folder. For this tutorial, you can safely ignore all the questions that NPM will ask and press Enter for each of them. You can also run
npm init --yes to automatically answer all the questions with the default values.
index.js and put it next to our
In this file, we need to load the Express library by using Node.js'
It will allow us to instantiate the library and listen on a port on the current machine (your development computer or a server if the code is deployed).
To instantiate an instance of Express, we need to execute the function that was returned by the library and store the result in a variable. Then, we can listen to a specific port on the machine, here port 3000, using the Express instance (
We are now ready to add API routes to our small server by using Express methods that match the HTTP methods or verbs (
POST, etc.). Depending on the route you want to create,
POST /invoices, etc. you can use the corresponding method with the path as a parameter:
Of course, this code is quite basic, and the two routes we created should probably load the list of users and delete users from a database before returning a result. But this is out of the scope of this small tutorial.
Your API is now available on http://localhost:3000. You can do a test call to the following API endpoints
GET /users and
DELETE /users using your favorite tool (here Insomnia) and see the returned response:
Working with an API can be challenging. It could be unavailable for various reasons: the whole API is under development, some routes are missing, the documentation is outdated, the access is restricted to the production environment or behind a firewall, etc.
Instead of waiting for the API to be ready to be able to consume it, you could mock it using an API mocking tool like Mockoon.
API mocking is a technique that consists in imitating an unavailable API by simulating the endpoints and their responses. With this technique, you can have a running mock in no time and start calling it right away from your front-end or back-end application.
Mocking an API with Mockoon is easy and requires only some small steps to start working.
To learn more about setting up Mockoon and creating your first fake API in less than 5 minutes, head over to our getting start tutorial
API mocking with Mockoon