Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect Location but not URL

How do I redirect the user to a different route without redirecting the URL in vanilla Express? For example, if I have the 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 has changed, as well, to http://localhost:3000/new. How do I get the application to redirect to the new route but keep the old URL (http://localhost:3000/old)?

I'm fine installing another middleware (as if I don't already have a million already), but ideally, I'd like to do this without extra middleware. Also, I'm doing this completely in Express.js, without PHP, nginx, or anything else. I will be using Angular.js in my application, but this seems like more of a server-side behavior, rather than client-side. I do redirect on the client side sometimes, but I don't want to do it all the time.

like image 429
trysis Avatar asked Jul 26 '14 17:07

trysis


People also ask

How do I redirect a specific URL?

Add a new URL redirectClick the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.

How do I redirect a Windows location?

window. location. href = "https://www.example.com"; Simply insert your target URL that you want to redirect to in the above code.


1 Answers

rather than redirect, how about just send a get request using request module

and do a callback to update 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);
    }
  });         
});
like image 185
Ian Wang Avatar answered Oct 19 '22 19:10

Ian Wang