【发布时间】:2013-02-01 07:11:19
【问题描述】:
我正在尝试使用 express 来解析查询字符串,以防在实际路由发生之前设置某些参数并执行一小段代码。用例是获取某个值,该值可以设置,与正在使用的链接无关。我使用 express 功能通过 next() 将内容传递给下一个可能的规则。
到目前为止,我尝试过 - 在所有 app.get/post-rule-block 的最顶部:
app.get('[?&]something=([^&#]*)', function(req, res, next) {
var somethingID = req.params.something;
// Below line is just there to illustrate that it's working. Actual code will do something real, of course.
console.log("Something: "+somethingID);
next();
})
app.get('/', site.index);
还有:
app.param('something', function(req, res, next) {
var somethingID = req.params.something;
console.log("Something: "+somethingID);
next();
})
app.get('/', site.index);
应该触发的示例:
URL: www.example.com/?something=10239
URL: www.example.com/superpage/?something=10239
URL: www.example.com/minisite/?anything=10&something=10239
不幸的是,我的解决方案都没有真正起作用,所发生的只是触发了下一个匹配规则,但上面的小函数从未执行过。有人知道如何做到这一点吗?
编辑:我明白,param-example 不起作用,因为之后我没有在任何其他路由规则中使用所述参数,它只会在那时被触发。
我也明白,这个逻辑暗示,Express 忽略查询字符串,它通常在路由已经发生后在函数内解析。但如前所述,我需要它是“与路由无关的”,并且可以使用在此应用程序中处理的任何 URL。
【问题讨论】:
标签: regex node.js routing express