如何使用Node.JS的Restify框架解析/读取多个参数 [英] How to parse/read multiple parameters with restify framework for Node.JS

查看:210
本文介绍了如何使用Node.JS的Restify框架解析/读取多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:

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

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

我能够处理以下网址:

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

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

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

I can parse/read the parameter from the above URL

代码:

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);
});

问题:如何从格式为http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324

推荐答案

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

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

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

这篇关于如何使用Node.JS的Restify框架解析/读取多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆