Free Ebook cover How to create APIs in NodeJS from basic to advanced

How to create APIs in NodeJS from basic to advanced

5

(1)

149 pages

Working with routes in ExpressJS: Using HTTP verbs in routes (GET, POST, PUT, DELETE)

Capítulo 54

Estimated reading time: 4 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Working with routes in ExpressJS is one of the most fundamental parts of developing APIs in NodeJS. ExpressJS is a minimal and flexible Node.js web app framework that provides a robust set of features for web and mobile apps. Routes are used to define different URL endpoints that the API can respond to. Each route can have one or more handler functions, which are executed when the route is matched.

Routes in ExpressJS can be defined using different methods corresponding to HTTP verbs - GET, POST, PUT and DELETE. These HTTP verbs are the four main request methods that represent the action to be performed on the specified resource.

GET

The GET method is used to request data from a specified resource. In ExpressJS, we can define a GET route as follows:

```javascript app.get('/path', function(req, res) { res.send('GET request to the homepage'); }); ```

In this example, '/path' is the route or URL. When a client sends a GET request to this URL, the callback function is called. This function takes the request object and the response object as arguments, and we can send a response using the 'send' method of the response object.

POST

The POST method is used to send data to be processed to a specified resource. Here is an example of how to define a POST route in ExpressJS:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

```javascript app.post('/path', function(req, res) { res.send('POST request to the homepage'); }); ```

As with the GET method, '/path' is the route the client is sending the POST request to. The callback function is called when the POST request is received, and we can send a response back to the client.

PUT

The PUT method is used to update the specified resource. Here's an example of how to define a PUT route in ExpressJS:

```javascript app.put('/path', function(req, res) { res.send('PUT request to the homepage'); }); ```

In this example, '/path' is the route where the client is sending the PUT request. When that request is received, the callback function is called and we can send a response to the client.

DELETE

The DELETE method is used to delete the specified resource. Here is an example of how to define a DELETE route in ExpressJS:

```javascript app.delete('/path', function(req, res) { res.send('DELETE request to the homepage'); }); ```

As in the previous examples, '/path' is the route where the client is sending the DELETE request. When the request is received, the callback function is called and we can send a response to the client.

In short, routes in ExpressJS are defined using methods that correspond to HTTP verbs. Each route has one or more handler functions that are called when the route is matched. These handler functions receive request and response objects, and may send a response back to the client.

Working with routes in ExpressJS is an essential part of developing APIs in NodeJS. Understanding how to define routes and how they work is crucial to building robust and efficient APIs. Therefore, it is important that you practice creating routes and experiment with different methods and routes to become familiar with them.

Finally, remember that even though we are using ExpressJS in this example, the concept of HTTP routes and verbs is the same across all frameworks and programming languages. So the knowledge you gain here can be applied anywhere.

Now answer the exercise about the content:

What is the role of HTTP verbs in ExpressJS and how are they used to define routes?

You are right! Congratulations, now go to the next page

You missed! Try again.

HTTP verbs in ExpressJS define the type of action to perform on a resource, such as retrieving with GET, creating with POST, updating with PUT, and deleting with DELETE. They are essential in route definition, dictating how the server should handle requests at specific endpoints.

Next chapter

Working with routes in ExpressJS: Working with query parameters in routes

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.