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

Página 54

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:

```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.

Next page of the Free Ebook:

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

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text