如何在没有 Express 的情况下响应 Nodejs 中的动态内容
How to response dynamic content in Nodejs without Express
let reqBody = "";
req
.on("data", (chunk) => {
reqBody += chunk.toString();
})
.on("end", () => {
const body = new URLSearchParams(reqBody);
res.end("ok");
});
我试图在没有 Express 的情况下使用 NodeJS 访问请求的主体,但由于某些原因,我无法 运行 res.end("ok");
.
你也需要打电话给req.end()
检查Node.js文档
https://nodejs.org/api/http.html
With http.request() one must always call req.end() to signify the end
of the request - even if there is no data being written to the request
body
let reqBody = "";
req
.on("data", (chunk) => {
reqBody += chunk.toString();
})
.on("end", () => {
const body = new URLSearchParams(reqBody);
res.end("ok");
});
req.end();
let reqBody = "";
req
.on("data", (chunk) => {
reqBody += chunk.toString();
})
.on("end", () => {
const body = new URLSearchParams(reqBody);
res.end("ok");
});
我试图在没有 Express 的情况下使用 NodeJS 访问请求的主体,但由于某些原因,我无法 运行 res.end("ok");
.
你也需要打电话给req.end()
检查Node.js文档 https://nodejs.org/api/http.html
With http.request() one must always call req.end() to signify the end of the request - even if there is no data being written to the request body
let reqBody = "";
req
.on("data", (chunk) => {
reqBody += chunk.toString();
})
.on("end", () => {
const body = new URLSearchParams(reqBody);
res.end("ok");
});
req.end();