[英]Is there a way to do dynamic key/value parameters in url route in express.js
I am looking for a way (and hoping there is one available already) to read dynamically routed parameters in nodejs. 我正在寻找一种方法(并希望已经有一种方法)来读取nodejs中的动态路由参数。 (Like the way Zend Framework 1 default router did).
(就像Zend Framework 1默认路由器一样)。
So I want to have something like this: 所以我想要这样的东西:
app.get('/resource/:key/:value/:anotherkey/:differentvalue', function(req, res) {
return res.send('This will print the :differentvalue: '+req.params.anotherkey);
});
But then without having to define all different keys and values 但随后不必定义所有不同的键和值
Try 尝试
app.get('/resource/*', function(req, res) {
var list = req.path.split('/').slice(2); // Slice to leave off '/resource/'
var obj {};
async.whilst(
function() { return list.length > 0 },
function(callback) {
obj[list.shift()] = list.shift();
},
function(err) { // return your obj here or do whatever else you need to do }
);
});
I would use the querystring for this particular usecase. 我将在这个特定用例中使用querystring。
So given a URL like this: myapp.com/search/?filter_one=param&filter_two=param2
因此,给定这样的URL:
myapp.com/search/?filter_one=param&filter_two=param2
And then use the URL module in node. 然后在节点中使用URL模块。
var url = require('url');
var parsed_url = url.parse(request.url, true);
var querystring_object = parsed_url.query
//{filter_one: "param", filter_two: "param2"}
//and that object is quite easier to hand off to elastic search
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.