簡體   English   中英

Node.js HTTP字符串

[英]Node.js HTTP Strings

給定完整的HTTP消息字符串,Node.js中是否有設施可以對此進行解析? 基本上,我不會使用http.createServer函數。 我已經通過其他方式(ZMQ)獲得了完整的HTTP消息,但是我需要對其進行解析,這意味着標頭,查詢字符串,發布正文等。

此外,相同的功能應該能夠逆轉該過程,即創建完整格式的HTTP響應消息字符串。 我可以使用並通過不同的傳輸方式(ZMQ)。

我知道PHP具有Symfony的HTTP Foundation可以滿足我的要求)。 Node.js中是否有類似的東西?

我發現了這個: https : //github.com/joyent/http-parser但它在C ++中

您可以在節點內使用http解析器,但是它沒有很好的文檔說明。 我從測試中提取了以下示例:

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

提供以下輸出:

message complete
request method: POST
request body:

 foo=42&bar=1337

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM