【问题标题】:Express restart matching route - Redirect快速重启匹配路由 - 重定向
【发布时间】:2013-08-19 15:11:51
【问题描述】:

我想在处理请求期间更改 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”。

【问题讨论】:

    标签: javascript http express


    【解决方案1】:

    使用req.session 跨重定向跟踪对象。您的问题不清楚重定向的作用,但如果您不需要保存任何内容,则可以跳过 req.session 并简单地从/r/* 的请求处理函数内部执行res.redirect("/myverylongurl/param1.....")

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

    【讨论】:

    • 不幸的是,我需要在不利用客户端的情况下进行内部重定向。在这种情况下,您向客户端发送 301 http 请求,这不是我需要的。
    • AFAIK 没有办法从另一个 Express 路由处理程序内部执行一个 Express 路由处理程序内部的代码,而没有 301 重定向来调用该路由。您可能应该将 #complex code here 移到它自己的函数中,并从 /r/* 和 `/myverylongurl?param1=true' 处理程序中调用该函数。
    【解决方案2】:

    听起来您正在寻找使用相同代码处理两条路线。如果您不想将 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');
    });
    

    【讨论】:

    • 我当然知道,这很明显。但我正在寻找一种避免这种情况的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2017-12-21
    • 2020-04-03
    相关资源
    最近更新 更多