Express res.location() Method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code Examples

What you’ll learn

  • How to set the Location response header with res.location().
  • How res.location() differs from res.redirect().
  • How to use it in resource creation and navigation flows.
  • How to combine it with response status codes safely.

Syntax

javascript
res.location(path)
1

Set Location after creating resource

javascript
app.post('/users', function (req, res) {
  var newId = 101;
  res.location('/users/' + newId);
  res.status(201).json({ id: newId });
});
2

Set location then redirect

javascript
app.get('/go-dashboard', function (req, res) {
  res.location('/dashboard');
  res.redirect(302, '/dashboard');
});
3

Set location for client-side follow-up

javascript
app.put('/profile', function (req, res) {
  res.location('/profile');
  res.status(200).json({ message: 'Profile updated' });
});

⚠️ Common pitfalls

  • res.location() alone does not redirect; you must also send/end/redirect explicitly.
  • Set location before sending the final response to ensure headers are included.
  • Use clear paths/URLs and avoid untrusted direct user input in location values.

❓ FAQ

It sets the Location header in the outgoing response.
No. res.location() only sets the header; res.redirect() sets location and ends response with redirect status.
Yes, relative paths can be used, though absolute URLs may be clearer for some clients.
It is commonly used in create endpoints and redirect flows where clients should navigate to another URL.
No, it only sets the header. You still need to send/end the response.
Did you know?

res.location() sets the Location response header and is often used before redirects.

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.

4 people found this page helpful