[英]Node.js HTTP Strings
Given a full HTTP message string, is there facilities in Node.js to parse this? 给定完整的HTTP消息字符串,Node.js中是否有设施可以对此进行解析? Basically I'm not going to be using the http.createServer function.
基本上,我不会使用http.createServer函数。 I already have the full HTTP message through other means (ZMQ), but I need to parse it, which means headers, query strings, post body.. etc.
我已经通过其他方式(ZMQ)获得了完整的HTTP消息,但是我需要对其进行解析,这意味着标头,查询字符串,发布正文等。
Furthermore, the same functionality should be able to reverse the process, that is creating a fully formed HTTP response message string. 此外,相同的功能应该能够逆转该过程,即创建完整格式的HTTP响应消息字符串。 That I can use and pass through different transport (ZMQ).
我可以使用并通过不同的传输方式(ZMQ)。
I know that PHP has Symfony's HTTP Foundation (which does what I'm asking). 我知道PHP具有Symfony的HTTP Foundation ( 可以满足我的要求)。 Is there something in Node.js that does a similar thing?
Node.js中是否有类似的东西?
I found this: https://github.com/joyent/http-parser but it's in C++ 我发现了这个: https : //github.com/joyent/http-parser但它在C ++中
You can use the http-parser from within node, but it's not very well documented. 您可以在节点内使用http解析器,但是它没有很好的文档说明。 I extracted the following example from the tests :
我从测试中提取了以下示例:
var HTTPParser = process.binding('http_parser').HTTPParser,
CRLF = '\r\n',
request = new Buffer('POST /it HTTP/1.1' + CRLF +
'Content-Type: application/x-www-form-urlencoded' + CRLF +
'Content-Length: 15' + CRLF +
CRLF +
'foo=42&bar=1337'),
parser = new HTTPParser(HTTPParser.REQUEST),
headers = {},
body = '';
parser.onHeadersComplete = function(info) {
headers = info;
};
parser.onBody = function(b, start, len) {
body = b.slice(start, start + len).toString();
};
parser.onMessageComplete = function() {
console.log('message complete');
console.log('request method: ' + headers.method);
console.log('request body:\n\n ' + body);
};
parser.execute(request, 0, request.length);
Gives the following output: 提供以下输出:
message complete
request method: POST
request body:
foo=42&bar=1337
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.