In Express.js, res.location(path) sets the response Location HTTP header to the given path or URL. It returns res for chaining. It does not send the body or end the response — you still call res.send, res.json, res.end, or similar.
Use it when clients need a URL (for example a new resource after 201 Created). For an automatic browser redirect, prefer res.redirect(), which sets Location, status, and finishes the response.
Syntax
Call res.location in your Express.js app. Start with this line.
GET /go
→ 200
Location: /foo/bar
(empty body)
Note: browsers do not auto-navigate on 200 + Location.
Use res.redirect() when you need a redirect.
How It Works
Set the header.res.location('/foo/bar') writes Location: /foo/bar.
End the response. Without end/send/json, the client waits forever.
Status stays default. Here that is 200 unless you call res.status.
Not a redirect. Clients may read the header; they will not follow it like a 3xx.
Example 2 — 201 Created with Resource URL
Classic API pattern: point Location at the new resource, return 201 and a JSON body.
Express
const express = require('express');
const app = express();
app.use(express.json());
app.post('/users', (req, res) => {
const id = 42; // pretend insert returned an id
const url = '/users/' + id;
res.location(url);
res.status(201).json({ id, url });
});
app.listen(3000);
Output
POST /users
→ 201 Created
Location: /users/42
{ "id": 42, "url": "/users/42" }
How It Works
Create the resource. Your app assigns an id (database, etc.).
Advertise the URL.res.location('/users/' + id) sets the header clients expect.
Use 201.res.status(201) means “created,” not a temporary redirect.
Send JSON. Body can repeat the id/url for clients that ignore headers.
Example 3 — Previous Page without 'back'
Express 5 replacement for the removed magic string: Referrer header, or home.
Express
const express = require('express');
const app = express();
app.get('/done', (req, res) => {
// Express 5: do NOT use res.location('back')
const target = req.get('Referrer') || '/';
res.location(target);
res.status(200).send('OK — see Location header');
});
// Or, if you want an actual redirect:
// res.redirect(req.get('Referrer') || '/');
app.listen(3000);
Output
GET /done
Referer: https://example.com/form
→ 200
Location: https://example.com/form
OK — see Location header
(no Referer → Location: /)
How It Works
Read Referrer.req.get('Referrer') reads the browser’s previous page (Express accepts the common misspelling too).
Fallback. If missing, use '/' so Location is never empty.
Set Location. Same idea as old 'back', but explicit and Express 5–safe.
Redirect when needed. Swap to res.redirect(target) if the client should navigate automatically.
Compatibility
Runtime Support
res.location() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only see the Location header on the HTTP response.
✓ Server · Express 4 & 5
res.location()
Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.
ServerNode.js runtime API
Node.js18+ for Express 5 · 14+ typical for Express 4
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
res.location()Express on Node.js
Bottom line: Study and run these examples with Node.js and the express package. Prefer Express 5–safe Referrer handling instead of 'back'.
Frequently Asked Questions
It sets the Location HTTP response header to the given path or URL. It does not end the response by itself.
res.location() only sets the header. res.redirect() sets Location, chooses a redirect status (default 302), and ends the response.
In Express 5, the magic string back is removed. Use res.location(req.get('Referrer') || '/') instead. In Express 4, back was deprecated.
Commonly with 201 Created: set Location to the new resource URL, then send JSON or an empty body with status 201.
No. After encoding if needed, Express passes the value to the browser without validation. Relative URLs are resolved by the client.