7.6. Working with routes in ExpressJS: Creating routes for CRUD

Página 52

ExpressJS is a framework for Node.js that provides robust features for building web and mobile APIs. One of the main features of ExpressJS is the ease of defining and manipulating routes. In this chapter, we will discuss how to work with routes in ExpressJS, specifically how to create routes for CRUD operations - Create, Read, Update and Delete.

Introduction

Routes are URLs or URIs that direct users to different parts of a web application. In ExpressJS, routes are defined using HTTP methods (GET, POST, PUT, DELETE) and the URL of the route. Each route can have one or more handler functions, which are executed when the route is triggered.

Creating Routes for CRUD Operations

Create

To create a route that handles a POST request (used to send data), we use the app.post() method. For example, to create a route to add a new item, we could do the following:

app.post('/items', function(req, res) {
  // code to add a new item
});

The first argument is the URL of the route. The second argument is a callback function that is called when the route is triggered. The callback function takes two arguments: a request object (req) and a response object (res). The request object contains information about the HTTP request, including the data sent by the client. The response object is used to send a response to the client.

Read (Read)

To create a route that handles a GET request (used to fetch data), we use the app.get() method. For example, to create a route to fetch all items, we could do the following:

app.get('/items', function(req, res) {
  // code to fetch all items
});

To fetch a single item, we could add a route parameter to the URL, like this:

app.get('/items/:id', function(req, res) {
  // code to search for an item by id
});

The route parameter is defined using a colon followed by the parameter name (:id). The value of the route parameter can be accessed using req.params.id.

Update

To create a route that handles a PUT request (used to update data), we use the app.put() method. For example, to create a route to update an item, we could do the following:

app.put('/items/:id', function(req, res) {
  // code to update an item by id
});

Delete

To create a route that handles a DELETE request (used to delete data), we use the app.delete() method. For example, to create a route to delete an item, we could do the following:

app.delete('/items/:id', function(req, res) {
  // code to delete an item by id
});

Conclusion

Working with routes in ExpressJS is very simple and intuitive. By creating routes for CRUD operations, we can easily manipulate data and interact with our database. In the next chapter, we'll discuss how to connect our ExpressJS application to a MongoDB database to store and retrieve our data.

Now answer the exercise about the content:

What HTTP method and ExpressJS function are used to create a route that handles data deletion in a web application?

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

You missed! Try again.

Next page of the Free Ebook:

537.7. Working with routes in ExpressJS: Organizing routes in separate files

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