0

此代码正在从 curl 接收数据,并假设在标头正文响应中显示该数据。但它不起作用。我哪里错了???

const server = http.createServer((req , res) => {
res.writeHead(200, {'Content-type': 'text/plain'});
const { headers, method, url } = req;
let body = [];
req.on('error', (err) => {
    console.error(err);
  })
req.on('data', (chunk) => {
body.push(chunk);
})
req.on('end', () => {
    body = Buffer.concat(body).toString();
});

});

4

1 回答 1

0

如果您All together now!要在响应正文中设置什么,这应该可以完成工作。

const http = require('http');

const server = http.createServer((req, res) => {
    let body = [];
    req.on('error', (err) => {
        console.error(err);
    })
    req.on('data', (chunk) => {
        body.push(chunk);
    })
    req.on('end', () => {
        body = Buffer.concat(body).toString();

        // set response
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end(body);
    });
});

server.listen('3000');
于 2020-09-06T15:00:25.353 回答