Express res.location() Method
What you’ll learn
- How to set the
Locationresponse header withres.location(). - How
res.location()differs fromres.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.
4 people found this page helpful
