REST (Representational State Transfer) is a popular architectural style used to design web services and APIs. It provides a standardized way of exchanging data between different systems and applications. Node.js and Express are two of the most popular tools used to build RESTful APIs.
In this tutorial, we will learn how to build a RESTful API with Node.js and Express. We will cover the following topics:
- Setting up a Node.js project
- Installing and configuring Express
- Creating routes and handling requests
- Adding middleware
- Connecting to a database
- Implementing CRUD operations
- Testing the API
Setting up a Node.js project
Before we start building our API, we need to set up a Node.js project. If you don’t have Node.js installed on your computer, you can download it from the official website: https://nodejs.org.
Once Node.js is installed, open your terminal or command prompt and create a new directory for your project:
mkdir my-api
cd my-api
Next, initialize a new Node.js project using NPM (Node Package Manager):
npm init -y
This will create a default package.json
file with some basic settings. We can modify this file later to add dependencies and other configurations.
Installing and configuring Express
Express is a popular web framework for Node.js that simplifies the process of building web applications and APIs. To install it, run the following command in your project directory:
npm install express
This will download and install the latest version of Express and add it as a dependency to your package.json
file.
Once Express is installed, create a new file called index.js
in your project directory. This will be the entry point for our application. Add the following code to initialize a new Express app:
const express = require('express');
const app = express();
const port = 3000;
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
This code creates a new Express app and sets it up to listen on port 3000. Whenever a request is received on this port, the server will log a message to the console.
To test if Express is working correctly, run the following command:
node index.js
This should start the server and display the message “Server running at http://localhost:3000” in the console. If you visit this URL in your web browser, you should see an error message because we haven’t added any routes yet.
Creating routes and handling requests
In order to handle incoming requests to our API, we need to create routes. Routes are mappings between HTTP methods (such as GET, POST, PUT, DELETE) and URL paths. Each route defines a function that will be executed when the corresponding request is received.
To create a route in Express, we use the app.METHOD()
syntax, where METHOD is the HTTP method (in uppercase) and the path is specified as a string. For example, to create a route to handle GET requests to the root URL path (/
), we can add the following code:
app.get('/', (req, res) => {
res.send('Hello World!');
});
This defines a route that will handle GET requests to the root URL (/
) and respond with the message “Hello World!”.
We can create additional routes to handle other HTTP methods and URL paths. For example, to handle POST requests to the /users
path (for creating new user accounts), we can add the following code:
app.post('/users', (req, res) => {
// create new user account
res.send('User account created');
});
This code defines a route that will handle POST requests to the /users
path and create a new user account.
Adding middleware
Middleware functions are functions that are executed between the incoming request and the final response. They can be used to perform various tasks, such as logging, authentication, error handling, etc.
To add middleware in Express, we use the app.use()
method. We can pass one or more functions as arguments to this method, and they will be executed for every incoming request.
For example, to add a simple logger middleware that logs each incoming request to the console, we can add the following code:
app.use((req, res, next) => {
console.log(`${req.method} ${req.url} - ${new Date()}`);
next();
});
This code defines a middleware function that logs the HTTP method, URL, and current date and time for each incoming request.
We can also use third-party middleware libraries, such as body-parser
for parsing request bodies or cors
for handling cross-origin requests. To use these libraries, we need to install them as dependencies and require them in our code:
npm install body-parser cors
const bodyParser = require('body-parser');
const cors = require('cors');
app.use(bodyParser.json());
app.use(cors());
These lines of code add the body-parser
and cors
middleware to our Express app.
Connecting to a database
Most web applications and APIs need to store and retrieve data from a database. There are several databases that can be used with Node.js and Express, such as MySQL, PostgreSQL, MongoDB, etc.
For this tutorial, we will use MongoDB, a popular NoSQL database that stores data in a JSON-like format. To connect to MongoDB, we need to install the mongoose
package:
npm install mongoose
Then, we can require it in our code and connect to a MongoDB database:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my-api', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('Database connected');
}).catch((err) => {
console.error(err);
});
This code connects to a local MongoDB instance running on the default port (27017
) and a database called my-api
. If the connection is successful, it logs a message to the console.
Implementing CRUD operations
CRUD stands for Create, Read, Update, and Delete, which are the basic operations that can be performed on data in a database.
To implement these operations in our API, we need to create routes that handle HTTP requests for each operation. For example, to create a new user account, we can define a POST
route like this:
app.post('/users', (req, res) => {
const { name, email, password } = req.body;
const user = new User({
name,
email,
password
});
user.save().then(() => {
res.status(201).send(user);
}).catch((err) => {
console.error(err);
res.status(500).send('Error creating user');
});
});
This code extracts the name
, email
, and password
fields from the request body, creates a new User
object using Mongoose, and saves it to the database. If the operation is successful, it returns a 201 Created
status code and the user object in the response. If there is an error, it returns a 500 Internal Server Error
status code and an error message.
Similar routes can be defined for other CRUD operations, such as GET
to retrieve a list of users, PUT
to update a user account, and DELETE
to delete a user account. These routes will use Mongoose methods like find
, findOne
, update
, and remove
to interact with the database.
Testing the API
Finally, we need to test our API to make sure it works as expected. There are several tools and libraries that can be used for testing APIs, such as Postman, Newman, Mocha, etc.
For this tutorial, we will use Supertest, a library that allows us to send HTTP requests to our Express app and make assertions on the responses.
To install Supertest, run the following command:
npm install supertest --save-dev
Then, create a new file called test.js
in your project directory and add the following code:
const request = require('supertest');
const app = require('./index');
describe('Test API endpoints', () => {
it('should return Hello World message', async () => {
const res = await request(app).get('/');
expect(res.statusCode).toEqual(200);
expect(res.text).toEqual('Hello World!');
});
});
This code defines a simple test that sends a GET
request to the root URL path and verifies that the response has a 200 OK
status code and the message “Hello World!”.
To run the tests, execute the following command:
npm test
This should run the test and display the results in the console.
Conclusion
In this tutorial, we learned how to build a RESTful API with Node.js and Express. We covered several key topics, such as setting up a Node.js project, installing and configuring Express, creating routes and handling requests, adding middleware, connecting to a database, implementing CRUD operations, and testing the API.
By following this tutorial, you should now have a good understanding of how to build and test RESTful APIs using Node.js and Express. Of course, there are many other features and libraries that can be used to enhance your APIs, but these basics should be enough to get you started. Happy coding!