Redirect location but not URL

How to redirect a user to another route without redirecting the url in vanilla Express? For example, if I have routes

express = require('express'); app = express(); app.route('/old').get(function(req, res, next) { res.redirect('/new'); }); app.route('/new').get(function(req, res, next) { res.send('This is the new route'); }); app.listen(3000); 

and I point my browser to http://localhost:3000/old , the application successfully redirects, and the page says This is the new route , but the URL also changed to http://localhost:3000/new . How to make the application redirect the new route, but keep the old URL ( http://localhost:3000/old )?

I am fine at installing other middleware (as if I don’t already have a million), but ideally I would like to do this without additional middleware. In addition, I do it completely in Express.js, without PHP, nginx or anything else. I will use Angular.js in my application, but this is similar to the behavior on the server side, and not on the client side. Sometimes I do client-side redirection, but I don't want to do this all the time.

+7
redirect express
source share
2 answers

rather than redirecting how easy it is to send a receive request using the request module

and call back to update the content.

 var request = require('request'); app.route('/old').get(function(req, res, next) { request.get('/new', function(err, response, body) { if (!err) { req.send(body); } }); }); 
+6
source share

try it

 var request = require('request'); app.get('/old', function(req, res) { request.get('new', function(err, response, body) { if (!err) { res.send(body); } }); }); 
0
source share

All Articles