快速重启匹配路由 - 重定向

时间:2013-08-19 15:11:51

标签: javascript http express

我想在处理请求期间更改网址。实现简单的重定向。 如何更改当前网址并重新启动快递中路由匹配的处理?

例如,我希望有这样的东西:

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

以及route / myverylongurl /....

的代码处理程序中的某处
app.get "/myverylongurl/*",  (req, res, next) ->
    # complex thing here

所以当有人请求http://myserverfake111.com/r/param1时,系统实际上最终处理了“#complex thing here”。

2 个答案:

答案 0 :(得分:0)

使用req.session跟踪重定向中的对象。您的问题不清楚重定向的作用,但如果您不需要保存任何内容,则可以跳过req.session,只需从res.redirect("/myverylongurl/param1.....")的请求处理函数内部执行/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');
});

答案 1 :(得分:0)

听起来您正在寻找使用相同代码处理两条路线。如果您不想对res.redirect使用301重定向,请考虑组织代码,以便分离共享逻辑并为两个路由执行它。您仍然可以使用req.session来维护内部处理程序函数链中的变量作用域,即使它不发送重定向也是如此。或者,您可以使用回调将复杂代码放在其自己的函数中,并根据需要执行它。

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');
});