Chuck's Academy

Node.js

Creating a REST API with Express

Express is one of the most popular and widely used frameworks in Node.js for creating REST APIs. Its simplicity and flexibility allow building robust applications quickly. In this chapter, we will learn how to create a REST API using Express, handling routes and HTTP requests like GET, POST, PUT, and DELETE.

What is Express?

Express is a minimalist web framework for Node.js that provides a set of essential features for web application and API development. Through Express, we can manage routes, handle middleware, and process HTTP requests with great ease.

Installing Express

To use Express in our project, we first need to install it. If you haven't done so, make sure to initialize your Node.js project by running npm init -y. Then, install Express with the following command:

bash
"This command will install the express framework in your project. Run 'npm space install space express' from your terminal to add it to the project dependencies."

Setting up a basic server with Express

Once installed, let's set up a basic server using Express. Create a file named app.js and write the following code:

javascript
"In this example, we are importing the express module and creating an instance of our application named app. We define a route at the root with the get method, which will respond with the message 'Hello, world' when accessed. Finally, we start the server on port three thousand and display a message in the console to confirm it is running."

To run your server, use the following command in the terminal:

bash
"Execute 'node space app dot j s' in the terminal to start the server. Then you will be able to access the URL 'localhost three thousand' in your browser to see the 'Hello, world' message."

Routes and HTTP Methods

In a REST API, each route corresponds to a URL that the client can access, and HTTP methods like GET, POST, PUT, and DELETE define the operations that can be performed at that URL.

Handling GET requests

GET requests are used to retrieve information from the server. Let's see how to define a GET route that returns a list of users:

javascript
"Here we define a get route for the URL 'slash users'. The server will respond with an array of objects representing users, using the 'res dot json' method to send a response in JSON format."

Handling POST requests

POST requests are used to send data to the server, usually to create new resources. To handle a POST, we need middleware that allows us to read the request body, such as express.json().

javascript
"In this example, we use the 'post' method to handle requests to the 'slash users' URL. First, we enable the 'express dot json' middleware to read the data sent in the request body. Then, we respond with a status code two hundred one, indicating that the resource was created successfully."

Handling PUT requests

PUT requests are used to update an existing resource. Here we see how to handle a PUT request to update a user:

javascript
"Here we define a put route to update a specific user by their ID. We use the dynamic 'id' parameter in the URL, which can be accessed using 'req dot params dot id'. The server responds with the updated user."

Handling DELETE requests

Lastly, DELETE requests are used to remove a resource. Here is an example to delete a user by their ID:

javascript
"This code handles a DELETE request to remove a specific user. Again we use 'req dot params dot id' to access the user's ID, and we respond with status code two hundred four, indicating that the operation was successful but there is no content in the response."

Summary

In this chapter, we have learned to create a basic REST API using Express. We covered how to handle HTTP GET, POST, PUT, and DELETE requests to interact with the server. Express allows us to create APIs in a simple and efficient way, making it an excellent tool for backend development.


Ask me anything