app.post()
Routes HTTP POST requests for a path to handler functions.
Syntax
app.post(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.post('/users', (req, res) => {
const user = { id: 1, ...req.body };
res.status(201).json(user);
});
Output
$ curl -X POST -d '{"name":"Ada"}' -H 'Content-Type: application/json' localhost:3000/users
{"id":1,"name":"Ada"}
app.post('/login', async (req, res) => {
const ok = await checkCredentials(req.body);
res.sendStatus(ok ? 204 : 401);
});
Output
$ curl -X POST ... localhost:3000/login
(204 No Content)
Notes
Add a body parser (`express.json()` or `express.urlencoded()`)
before the route or `req.body` is undefined. In Express 5 async
handlers that reject are forwarded to error middleware
automatically; in Express 4 you must catch and call `next(err)`.