express app.put() route

What is express app.put() route?

The Express.js framework provides a convenient method for handling HTTP PUT requests through the use of the app.put() function. The app.put() function allows us to specify a route path and a callback function that will be executed when an HTTP PUT request with the specified path is received by the server.


app.put(path, callback)


Suppose we want to create a route for updating user information. We can use app.put() function to handle the PUT request for this route:

app.put('/users/:id', function(req, res) {
   // Code to update user information

The above code defines a route for updating user information with a URL path of /users/:id. The :id part in the URL is a parameter that can be accessed using in the callback function.

In the callback function, we can write the code to update the user information in the database or perform any other required operation.

The app.put() function can also be used with middleware functions to perform operations like data validation, authentication, or logging before executing the main callback function. This can be achieved by defining the middleware functions as additional arguments to the app.put() function:

app.put('/users/:id', authenticateUser, validateData, function(req, res) {
   // Code to update user information

function authenticateUser(req, res, next) {
   // Code to authenticate user

function validateData(req, res, next) {
   // Code to validate user data

In the above code, the authenticateUser and validateData functions act as middleware functions that will be executed before the main callback function for the /users/:id route.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]