0

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

例如,我想要这样的东西:

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

以及路由 /myverylongurl/.... 的代码处理程序中的某处

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

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

4

2 回答 2

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');
});
于 2013-08-19T16:56:06.867 回答
0

听起来您正在寻找使用相同代码处理两条路线。如果您不想使用 301 重定向res.redirect,请考虑组织您的代码,以便分离共享逻辑并为两条路由执行它。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');
});
于 2013-08-19T17:22:24.053 回答