Express.js res.location() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

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.

Express
res.location(path)
res.location('/foo/bar');
res.location('http://example.com');
res.location('../login');
res.location(req.get('Referrer') || '/');
CallWhat it does
res.location(path)Primary signature.
res.location('/foo/bar');Related call form.
res.location('http://example.com');Related call form.
res.location('../login');Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

res.location(path)
res.location('/foo/bar');
res.location('http://example.com');
res.location('../login');
res.location(req.get('Referrer') || '/');

Parameters

ParameterDescription
pathRequired. URL to place in the Location header (absolute or relative).
ReturnsRequired. The response object, for chaining (for example .status(201).json(…)).

Return Value

res.location(url) returns res after setting the Location header. It does not change the status or end the response by itself.

Express
res.location('/users/42')
  .status(201)
  .json({ id: 42 });

Express.js Examples

Example 1 — Set the Location Header

Call res.location, then finish with res.end() (or send a body).

Express
const express = require('express');
const app = express();

app.get('/go', (req, res) => {
  res.location('/foo/bar');
  res.end(); // Location: /foo/bar
});

app.listen(3000);

How It Works

  1. Set the header. res.location('/foo/bar') writes Location: /foo/bar.
  2. End the response. Without end/send/json, the client waits forever.
  3. Status stays default. Here that is 200 unless you call res.status.
  4. 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);

How It Works

  1. Create the resource. Your app assigns an id (database, etc.).
  2. Advertise the URL. res.location('/users/' + id) sets the header clients expect.
  3. Use 201. res.status(201) means “created,” not a temporary redirect.
  4. 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);

How It Works

  1. Read Referrer. req.get('Referrer') reads the browser’s previous page (Express accepts the common misspelling too).
  2. Fallback. If missing, use '/' so Location is never empty.
  3. Set Location. Same idea as old 'back', but explicit and Express 5–safe.
  4. Redirect when needed. Swap to res.redirect(target) if the client should navigate automatically.

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.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via 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.

Next: res.redirect()

Redirect the client with status and Location in one call.

res.redirect() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful