简体   繁体   中英

Express restart matching route - Redirect

I want to change url address during processing request. Implementation of simple redirect. How can I change the current url and restart processing of the route matching in express?

For instance I would like to have something like this:

app.get "/r/*",  (req, res, next) ->
   changeURL = "/myverylongurl/param1....."
   next(...)

and somewherein the code handler for route /myverylongurl/....

app.get "/myverylongurl/*",  (req, res, next) ->
    # complex thing here

So when someone request http://myserverfake111.com/r/param1 the system actually ends up processing block "# complex thing here".

Use req.session to keep track of objects across redirects. Your question isn't clear what the redirect does, but if you don't need to save anything you can skip req.session and simply do res.redirect("/myverylongurl/param1.....") from inside the request handler function of /r/* .

var http = require('http')
var express = require('express');
var app = module.exports = express();

app.use(express.logger());
app.use(express.cookieParser('asdf 0a98234ja       af anekjoavzcx'));
app.use(express.session());
app.use(app.router);

app.get('/'
,function(req, res){ 
  res.send(200, 'Hi, /') 
});

app.get("/r/*"
,function(req, res, next){
  //res.redirect('/myverylongurl?param1=true');
  req.session.changeURL = "/myverylongurl?param1=true";
  next();
},function(req, res, next){
  res.redirect('/foobar');
});

app.get('/foobar', function(req, res){
  var destination = req.session.changeURL || "/";
  if(req.session.changeURL){ delete req.session.changeURL };
  res.redirect(destination);
});

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

http.createServer(app).listen(3000, function(){
  console.log('Express server listening on port 3000');
});

Sounds like you are looking to process two routes with the same code. If you don't want to use a 301 redirect with res.redirect , consider organizing your code so you separate the shared logic and execute it for both routes. You can still use req.session to maintain variable scope along an internal handler function chain, even if it doesn't send a redirect. Or, you may be able to place the complex code in its own function with a callback and execute it on demand.

var http = require('http')
var express = require('express');
var app = module.exports = express();

app.use(express.logger('dev'));
app.use(express.cookieParser('asdf 0a98234ja       af anekjoavzcx'));
app.use(express.session());
app.use(app.router);

app.get('/' ,function(req, res){ res.send(200, 'Hi, /') });

// strategy 1
function complexThings1(req, res, next){
  console.log('#complex stuff started')
  process.nextTick(function(){ // example asynchronous function
    req.session.complexOutput="2i+3";
    console.log('#complex stuff finished')
    next();
  });
};

app.get('/myverylongurl'
,complexThings1
,function(req, res){
  res.send(200, 'complex things 1 output: '+req.session.complexOutput);
});

app.get("/r/*"
,complexThings1
,function(req, res){
  res.send(200, 'complex things 1 output: '+req.session.complexOutput);
});

// strategy 2
function complexThings2(input, callback){
  console.log('#complex stuff finished');
  process.nextTick(function(){ // example asynchronous function
    console.log('#complex stuff finished');
    var output = input
    callback(null, output);
  });
};

app.get('/s/*', function(req, res){
  complexThings2('8i+3.14', function(err, output){
    res.send(200, 'complex things 2 output: '+output);
  });
});

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

http.createServer(app).listen(3000, function(){
  console.log('Express server listening on port 3000');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM