Node.js忽略除第一个之外的所有查询字符串参数

时间:2013-07-05 10:36:01

标签: javascript node.js rest curl query-string

我无法说服Node.js查看我的网址中的第二个参数:

http://localhost:8888/cities?tag=123&date=2013-03-30T10:00:28.000%2B02:00

我使用以下命令启动Node.js服务器:

node --debug ./router_rest.js

我的“router_rest.js”文件中有以下代码:

require("http").createServer(function (req, res) {
  //  For PUT/POST methods, wait until the
  //  complete request body has been read.
  if (req.method==="POST" || req.method==="PUT") {
    var body = "";
    req.on("data", function(data){
      body += data;
    })

    req.on("end", function(){
      return routeCall(req, res, body);
    })

  } else {
    return routeCall(req, res, "");
  }
}).listen(8888);

我从命令行进行以下调用:

curl http://localhost:8888/cities?tag=123&date=2013-03-30T10:00:28.000%2B02:00

当我调试并检查'req'参数时(在上面的匿名函数中),url属性显示为:

/cities?tag=123

我原以为报道的网址是:

/cities?tag=123&date=2013-03-30T10:00:28.000%2B02:00

即使我切换参数,节点仍然只能看到第一个参数。

为什么最后一个参数(即日期)被截断了?

1 个答案:

答案 0 :(得分:9)

节点很好。

curl命令中的第一个&符号导致shell在后台运行curl。在URL周围添加引号,它将起作用。

curl "http://localhost:8888/cities?tag=123&date=2013-03-30T10:00:28.000%2B02:00"