Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:读取node.js HTTP消息_Javascript_Node.js_Http_Http Get - Fatal编程技术网

Javascript:读取node.js HTTP消息

Javascript:读取node.js HTTP消息,javascript,node.js,http,http-get,Javascript,Node.js,Http,Http Get,我有一个包含以下信息的HTTP数据包: GET /something?name=joe HTTP/1.1 Host: hostname:8080 Connection: keep-alive ... 使用node.js,如何提取名称字段?我试着查看消息正文,发现它是空的,我不确定还有什么其他选项,或者如何获得这个值。提前感谢您并不总是需要使用Express for simple http server解决方案,您还可以使用内置模块: 请求的结果:http://example:8080/user

我有一个包含以下信息的HTTP数据包:

GET /something?name=joe HTTP/1.1
Host: hostname:8080
Connection: keep-alive
...

使用
node.js
,如何提取名称字段?我试着查看消息正文,发现它是空的,我不确定还有什么其他选项,或者如何获得这个值。提前感谢

您并不总是需要使用Express for simple http server解决方案,您还可以使用内置模块:

请求的结果:
http://example:8080/user?name=Mike&age=32

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

http.createServer(function(req, res) {
    var path = '',
        parsed = url.parse(req.url, true);

    if (parsed.pathname === '/user') {

        // Requested:  { protocol: null,
        //   slashes: null,
        //   auth: null,
        //   host: null,
        //   port: null,
        //   hostname: null,
        //   hash: null,
        //   search: '?name=Mike&age=32',
        //   query: { name: 'Mike', age: '32' },
        //   pathname: '/user',
        //   path: '/user?name=Mike&age=32',
        //   href: '/user?name=Mike&age=32' }
        console.log('Requested: ', parsed);
        path = parsed.pathname;
    }

    res.end('Found path ' + path);
}).listen(8080);

你能分享到目前为止你拥有的相关代码吗?您是否使用任何特定的HTTP服务器库或框架?有些人将创建一个
请求.查询
对象,该对象的属性为
名称
。但是,如果您只是使用Node自己的API,那么您必须自己进行修改。