高级前端
node
【Q633】Node 中服务端框架如何解析 http 的请求体 body

Node 中服务端框架如何解析 http 的请求体 body

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 651 (opens in a new tab)

Author 回答者: shfshanyue (opens in a new tab)

在 Node 服务中,通过 http.createServer 接收到的 req 为可读流,对流进行读取数据

const server = http.createServer((req, res) => {
  let body = "";
  req.on("data", (chunk) => (body += chunk));
  req.on("end", () => {
    data = body;
    res.end(data);
  });
});

Author 回答者: Asarua (opens in a new tab)

body-parser?