Javascript 参数回调未触发的nodejs expressjs路由

Javascript 参数回调未触发的nodejs expressjs路由,javascript,node.js,express,Javascript,Node.js,Express,在下面的代码中,我希望获得能够点击URL的功能:localhost:3000/verify、localhost:3000/verify/和localhost:3000/verify?hmac= 但是,对于这段代码,唯一有效的是中间选项。让这个路由按我预期的方式工作,我做错了什么 app.get('/', function (req, res) { res.send('Hello World!'); }); //add parameter routing app.param('hmac

在下面的代码中,我希望获得能够点击URL的功能:localhost:3000/verify、localhost:3000/verify/和localhost:3000/verify?hmac=

但是,对于这段代码,唯一有效的是中间选项。让这个路由按我预期的方式工作,我做错了什么

app.get('/', function (req, res) {

  res.send('Hello World!');
});



//add parameter routing
app.param('hmac', function(req, res, next, hmacvalue) {

    console.log("HMAC route hit " + hmacvalue);
    return next();

});

app.get('/verify/:hmac',function(req,res,next) {
    console.log("I'm in the verify route");
});

这是因为没有定义满足您要查找的条件的路由。路由/验证/:hmac与路由/验证不同。前者正在搜索一个模式,该模式需要一个值来代替:hmac

试试下面的方法

app.get('/verify',function(req, res, next) {
    console.log("I'm in the /verify route!");
    console.log(req.query.hmac + " I am the /verify?hmac=<>!");
});
请考虑阅读此处的快速路由文档:

路由/verify/:hmac仅与localhost:3000/verify/格式的URL匹配

如果你想捕获所有的URL,你必须为它们添加路由

app.get('/verify', function(req, res, next) {

    if ( req.query && req.query.hmac ) {
        console.log( 'localhost:3000/verify?hmac=<value>' );

        var hmac = req.query.hmac;
    } else {
        console.log('localhost:3000/verify');
    }
});

app.get('/verify/:hmac', function(req, res, next) {
    var hmac = req.params.hmac;
});

我有点困惑,你是否为另外两条添加了路线,或者这是唯一的路线?如果是后者,那么这条路线显然不匹配所有三个选项,只匹配中间的一个?这就是我的代码范围。我或多或少地遵循了这里的代码:在第二个例子中。好的,我可以试试。那么,我对app.param的预期用途遗漏了什么呢?我在处理多条具有类似参数的路线时,需要更多“一网打尽”类型的东西时,就使用了app.param。这在你的情况下是不必要的,我不经常使用它。