7.6. Working with routes in ExpressJS: Creating routes for CRUD
Page 52 | Listen in audio
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: