如何使用 Node.JS 的 restify 框架解析/读取多个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15830448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:07:30  来源:igfitidea点击:

How to parse/read multiple parameters with restify framework for Node.JS

node.jsquery-stringrequest.querystringquerystringparameterrestify

提问by Amol M Kulkarni

Scenario: We developer are trying to replace a web service (written in C#.Net) with Node.JS Restful API.

场景:我们的开发人员正在尝试用 Node.JS Restful API 替换一个 Web 服务(用 C#.Net 编写)。

Issue: Now we need to handle the incoming request as is (we don't have control over it). So the following is the format of the incoming URL:

问题:现在我们需要按原样处理传入的请求(我们无法控制它)。所以下面是传入URL的格式:

http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324&GetDetailType=FULL

http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324&GetDetailType=FULL

I am able to handle the URL like:

我能够处理这样的 URL:

http://www.website.com/service/Trans001/ae67ea324/FULL

http://www.website.com/service/Trans001/ae67ea324/FULL

I can parse/read the parameter from the above URL

我可以从上面的 URL 解析/读取参数

Code:

代码:

var server = require('restify').createServer();
function respond(req, res, next) {
    console.log("req.params.UID:" + req.params.UID);
    console.log("req.params.FacebookID:" + req.params.FacebookID);
    console.log("req.params.GetDetailType" + req.params.GetDetailType);
}
server.get('/service/:UID/:FacebookID/:GetDetailType', respond);
server.listen(8080, function () {
    console.log('%s listening at %s', server.name, server.url);
});

Question: How can I read the multiple parameters from the URL which is formatted like http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324

问题:如何从格式如下的 URL 中读取多个参数http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324

回答by Simon

You just need to load the query parser plugin like so;

你只需要像这样加载查询解析器插件;

server.use(restify.plugins.queryParser());

回答by kentor

Restify 5 (2017) answer:

Restify 5 (2017) 答案:

As of restify 5 you can now setup the query parser like this: server.use(restify.plugins.queryParser());.

随着5的RESTify你现在可以设置查询分析器这样的: server.use(restify.plugins.queryParser());

If you use this plugin you can access the parsed params in req.query.

如果你使用这个插件,你可以访问req.query.

For additional options and information, take a look into the restify documentation: http://restify.com/docs/plugins-api/#queryparser

有关其他选项和信息,请查看 restify 文档:http://restify.com/docs/plugins-api/#queryparser

回答by therightstuff

Simon's answer is no longer valid as restify's queryParser has been moved to the restify-plugins package. The updated solution is

西蒙的回答不再有效,因为 restify 的 queryParser 已移至 restify-plugins 包。更新的解决方案是

server.use(require('restify-plugins').queryParser());