【问题标题】:Passing route control with optional parameter after root in express?在express中的root之后使用可选参数传递路由控制?
【发布时间】:2011-10-10 17:10:48
【问题描述】:

我正在开发一个简单的 url-shortening 应用程序,并有以下快速路线:

app.get('/', function(req, res){
  res.render('index', {
    link: null
  });
});

app.post('/', function(req, res){
  function makeRandom(){
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 3 /*y u looking at me <33??*/; i++ )
      text += possible.charAt(Math.floor(Math.random() * possible.length));
    return text;
  }
  var url = req.body.user.url;
  var key = makeRandom();
  client.set(key, url);
  var link = 'http://50.22.248.74/l/' + key;
  res.render('index', {
    link: link
  });
  console.log(url);
  console.log(key);
});

app.get('/l/:key', function(req, res){
  client.get(req.params.key, function(err, reply){
    if(client.get(reply)){
      res.redirect(reply);
    }
    else{
      res.render('index', {
        link: null
      });
    }
  });
});

我想从我的路由中删除/l/(以使我的网址更短)并使 :key 参数可选。这是正确的方法吗:

app.get('/:key?', function(req, res, next){
  client.get(req.params.key, function(err, reply){
    if(client.get(reply)){
      res.redirect(reply);
    }
    else{
      next();
    }
  });
});

app.get('/', function(req, res){
  res.render('index, {
    link: null
  });
});

不确定我是否需要指定我的/ 路由是“下一个”路由。但由于我唯一的其他路线是我更新的帖子/ 路线,我想它会正常工作。

【问题讨论】:

    标签: node.js routes express


    【解决方案1】:

    这取决于 client.get 在传递 undefined 作为其第一个参数时所做的工作。

    这样的东西会更安全:

    app.get('/:key?', function(req, res, next) {
        var key = req.params.key;
        if (!key) {
            next();
            return;
        }
        client.get(key, function(err, reply) {
            if(client.get(reply)) {
                res.redirect(reply);
            }
            else {
                res.render('index', {
                    link: null
                });
            }
        });
    });
    

    在回调中调用next()没有问题。

    根据this,处理程序按照添加的顺序被调用,所以只要你的下一个路由是 app.get('/', ...) 如果没有键就会被调用。

    【讨论】:

    • 谢谢,伙计。你的另类也很感激。可悲的是,我还有另一个问题,但我认为就像您指出的那样,这是client.get 返回的结果。我抛出了一个cannot call method 'indexOf' of null 错误。
    • 另外,是否可以在else{} 中调用next()
    • 抱歉评论节大声笑。已经修好了,但它是超级 jank xD
    【解决方案2】:

    快递版:

    "dependencies": {
        "body-parser": "^1.19.0",
        "express": "^4.17.1"
      }
    

    可选参数非常方便,你可以使用express轻松声明和使用它们:

    app.get('/api/v1/tours/:cId/:pId/:batchNo?', (req, res)=>{
        console.log("category Id: "+req.params.cId);
        console.log("product ID: "+req.params.pId);
        if (req.params.batchNo){
            console.log("Batch No: "+req.params.batchNo);
        }
    });
    

    在上面的代码中,batchNo 是可选的。 Express 会认为它是可选的,因为在构建 URL 之后,我给出了一个“?” batchNo '/:batchNo?' 后的符号

    现在我可以只使用 categoryId 和 productId 或全部三个参数调用。

    http://127.0.0.1:3000/api/v1/tours/5/10
    //or
    http://127.0.0.1:3000/api/v1/tours/5/10/8987
    

    【讨论】:

      猜你喜欢
      • 2020-01-30
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 2017-10-02
      • 2016-04-20
      • 2015-12-16
      • 2022-10-02
      相关资源
      最近更新 更多