Get request body from node.js's http.IncomingMessage

后端 未结 2 657
清酒与你
清酒与你 2021-01-01 17:00

I\'m trying to implement a simple HTTP endpoint for an application written in node.js. I\'ve created the HTTP server, but now I\'m stuck on reading the request content body:

2条回答
  •  佛祖请我去吃肉
    2021-01-01 17:41

    'readable' event is wrong, it incorrectly adds an extra null character to the end of the body string

    Processing the stream with chunks using 'data' event:

    http.createServer((r, s) => {
        console.log(r.method, r.url, r.headers);
        let body = '';
        r.on('data', (chunk) => {
            body += chunk;
        });
        r.on('end', () => {
            console.log(body);
            s.write('OK'); 
            s.end(); 
        });
    }).listen(42646); 
    

提交回复
热议问题