Building RESTful APIs with Express.js

# Building RESTful APIs with Express.js

### Author: Son Nguyen

> Date: 2023-12-22

In modern web development, **RESTful APIs** are the backbone of communication between the client and server. One of the most popular frameworks for building these APIs is [Express.js](https://expressjs.com/), a minimal and flexible Node.js web application framework. In this article, we’ll dive deep into creating a RESTful API using Express.js, exploring its key components such as routing, middleware, error handling, and security best practices.

## What is a RESTful API?

A RESTful API (Representational State Transfer) is an architectural style for designing networked applications. It uses standard HTTP methods like `GET`, `POST`, `PUT`, and `DELETE` to interact with resources, and it leverages the stateless nature of HTTP for scalability and simplicity.

### Core Principles of REST:

- **Stateless Communication:** Each request from client to server must contain all the information needed to understand and process the request.
- **Uniform Interface:** A standardized way to interact with resources using HTTP verbs.
- **Resource-Based:** Everything is treated as a resource (e.g., users, posts, products).
- **Client-Server Architecture:** Separates the user interface concerns from the data storage concerns.

## Why Express.js?

Express.js is renowned for its simplicity and minimalism, making it a go-to choice for developers looking to create APIs quickly. Its robust set of features includes:

- **Routing:** Easy definition of routes to handle various HTTP methods.
- **Middleware Support:** A powerful mechanism to handle request processing, logging, authentication, etc.
- **Extensibility:** Integrates seamlessly with databases, templating engines, and other third-party libraries.

## Setting Up Your Express.js Project

Before diving into the code, ensure you have [Node.js](https://nodejs.org/) installed on your machine. Then, initialize your project and install Express.js:

```
# Initialize a new Node.js project
npm init -y

# Install Express.js
npm install express
```

## Creating a Basic Express Server

Let's start by creating a simple server. Create a file named `server.js` and add the following code:

```
const express = require("express");
const app = express();
const port = 3000;

// Middleware to parse JSON bodies
app.use(express.json());

// Basic route
app.get("/", (req, res) => {
res.send("Welcome to our RESTful API built with Express.js!");
});

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
```

This basic setup initializes an Express application, sets up a JSON parser middleware, defines a simple route, and starts the server.

## Defining API Routes

A RESTful API revolves around various endpoints. Let's create a set of routes to manage a resource - say, **users**.

### Creating a Users Router

Create a new folder called `routes` and inside it, create a file named `users.js`:

```
const express = require("express");
const router = express.Router();

// Sample in-memory user data
let users = [
{ id: 1, name: "Alice", email: "alice@example.com" },
{ id: 2, name: "Bob", email: "bob@example.com" },
];

// GET /users - Retrieve all users
router.get("/", (req, res) => {
res.json(users);
});

// GET /users/:id - Retrieve a single user by ID
router.get("/:id", (req, res) => {
const user = users.find((u) => u.id === parseInt(req.params.id));
if (!user) return res.status(404).json({ error: "User not found" });
res.json(user);
});

// POST /users - Create a new user
router.post("/", (req, res) => {
const { name, email } = req.body;
const newUser = { id: users.length + 1, name, email };
users.push(newUser);
res.status(201).json(newUser);
});

// PUT /users/:id - Update a user
router.put("/:id", (req, res) => {
const user = users.find((u) => u.id === parseInt(req.params.id));
if (!user) return res.status(404).json({ error: "User not found" });

const { name, email } = req.body;
user.name = name || user.name;
user.email = email || user.email;
res.json(user);
});

// DELETE /users/:id - Delete a user
router.delete("/:id", (req, res) => {
users = users.filter((u) => u.id !== parseInt(req.params.id));
res.status(204).send();
});

module.
```

### Integrating the Users Router

Now, integrate this router into your main `server.js` file:

```
const express = require("express");
const app = express();
const port = 3000;

// Import the users router
const usersRouter = require("./routes/users");

// Middleware to parse JSON bodies
app.use(express.json());

// Mount the users router at /users
app.use("/users", usersRouter);

// Basic route
app.get("/", (req, res) => {
res.send("Welcome to our RESTful API built with Express.js!");
});

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
```

## Middleware and Error Handling

Middleware functions in Express are functions that have access to the request object, respo