res.location()

Sets the Location response header without redirecting.

Since Express 4 Spec ↗

Syntax

res.location(path)

Parameters

NameTypeRequiredDescription
path string Yes The URL for the Location header, or `'back'`.

Returns

Response — The response object, for chaining.

Examples

app.post('/users', (req, res) => {
  const id = createUser(req.body);
  res.location(`/users/${id}`).status(201).json({ id });
});
Output
HTTP/1.1 201 Created
Location: /users/42
app.get('/x', (req, res) => {
  res.location('https://example.com');
  res.end();
});
Output
Location: https://example.com

Notes

Sets the header only - it does NOT change the status or redirect (use `res.redirect` for that). Commonly paired with 201 Created to point at the new resource, or 202 Accepted for async jobs.

See also