Node.js / Express路由与获取参数 [英] Node.js/Express routing with get params
本文介绍了Node.js / Express路由与获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
app.get('/ documents / format / type',function(req,res){
var format = req.params.format,
type = req.params.type;
});
所以如果我提出如
http:// localhost:3000 / documents / json / mini
我的格式和类型变量将分别为'json'和'mini',但如果我提出请求,如
http: // localhost:3000 / documents / mini / json
不。所以我的问题是:如何以不同的顺序获取相同的变量?
解决方案
你的路由不行,应该这样(用':')
app.get('/ documents /:format /:type' req,res){
var format = req.params.format,
type = req.params.type;
});
不幸的是你不能互换参数顺序。
有关 req.params
(和 req.query
)的更多信息,请查看api参考 here 。
Let's say I have get route like this:
app.get('/documents/format/type', function (req, res) {
var format = req.params.format,
type = req.params.type;
});
So if I make request like
http://localhost:3000/documents/json/mini
in my format and type variables will be 'json' and 'mini' respectively, but if I make request like
http://localhost:3000/documents/mini/json
not. So my question is: how can I get the same variables in different order?
解决方案
Your route isn't ok, it should be like this (with ':')
app.get('/documents/:format/:type', function (req, res) {
var format = req.params.format,
type = req.params.type;
});
Also you cannot interchange parameter order unfortunately.
For more information on req.params
(and req.query
) check out the api reference here.
这篇关于Node.js / Express路由与获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文