app.put()
Routes HTTP PUT requests for a path, typically for full resource replacement.
Syntax
app.put(path, ...handlers) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
path | string | RegExp | Yes | The route path. |
handlers | ...function | Yes | The handler chain. |
Returns
Application — The app instance, for chaining.
Examples
app.use(express.json());
app.put('/users/:id', (req, res) => {
const updated = replaceUser(req.params.id, req.body);
res.json(updated);
});
Output
$ curl -X PUT -d '{"name":"Grace"}' -H 'Content-Type: application/json' localhost:3000/users/1
{"id":"1","name":"Grace"}
app.put('/settings', (req, res) => {
saveSettings(req.body);
res.sendStatus(204);
});
Output
(204 No Content)
Notes
PUT semantically replaces the whole resource; use PATCH for partial
updates. PUT should be idempotent - repeating the same request must
leave the same state. Validate `req.body` before persisting.