完整的URL不会在node.js中传递

时间:2014-11-14 05:59:10

标签: javascript node.js

我正在使用以下代码

var http = require('http'),
    url = require('url');

function handle_incoming_request (req, res) {
    console.log('Incoming request: (' + req.method + ') ' + req.url);

    req.parsed_url = url.parse(req.url, true);
    console.log(req.parsed_url);
}

var server = http.createServer(handle_incoming_request);
server.listen(8080);

现在我curl下面

curl -i -X GET http://localhost:8080/albums/italy2012.json?page=0&page_size=20

然后我在节点控制台

下面
Incoming request: (GET) /albums/italy2012.json?page=0
{ protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: '?page=0',
  query: { page: '0' },
  pathname: '/albums/italy2012.json',
  path: '/albums/italy2012.json?page=0',
  href: '/albums/italy2012.json?page=0' }

正如你在第一行看到的那样,网址是一半。它省略了&page_size=20

为什么以及如何解决此问题?

1 个答案:

答案 0 :(得分:4)

问题在于你如何通过CURL发布,你需要引用URL,否则&符号会将CURL设置为后台进程

curl -i -X GET "http://localhost:8080/albums/italy2012.json?page=0&page_size=20"