Resource updates
Update a resource at a URL such as /users/:id.
Express.js · Module
Defines a route that handles HTTP PUT requests for a specified path. Express runs the supplied callback functions when the request method and path match.
app.put() registers one or more handlers for a specific URL path and matches only incoming HTTP PUT requests for that path. When a request matches, Express runs the callback with the request and response objects, allowing the handler to produce the response.
app.put(path, callback [, callback ...])| Name | Type | Description |
|---|---|---|
path | string or path pattern | The route path to match, such as /users/:id. |
callback functions | function | One or more callback functions invoked for a matching PUT request. |
Registers the PUT route on the application; the documentation does not specify a distinct return value for this method.
app.put().req and res, which provide request data and response operations.res.send().const express = require('express');
const app = express();
app.put('/users/:id', (req, res) => {
res.send(`Updated user ${req.params.id}`);
});
app.listen(3000);Creates a route that responds to PUT requests for a user identified by a path parameter.
Update a resource at a URL such as /users/:id.
Attach multiple processing stages before the final response handler.
Handle a PUT endpoint alongside GET, POST, and DELETE routes.
app.put() do?app.put(path, handler) defines a route for HTTP PUT requests at the specified path. Express invokes the handler when both the request method and route path match.
Follow-up: How would you support several methods on one path? Use app.route(path) and chain method-specific handlers such as .get(), .post(), and .put().
app.put()?The first argument identifies the route path, such as /users/:id. The callback receives the request and response objects and can send the result with methods such as res.send().
Follow-up: How do you read the :id value? Access the route parameter through req.params.id inside the handler.
app.put() relate to other application routing methods?Express provides explicit application-method entries for common HTTP methods, including app.put(). The method name corresponds to the lowercase HTTP method handled by the route.
Follow-up: How can middleware be applied at application level? Use app.use() or an application method with middleware functions before the final handler.
app.get()app.post()app.delete()app.route()app.use()reqres