我使用HTML5表单发出POST请求。但是当我在我的node.js req.on("readable", function(data))
上收到请求时,data
is undefined
这是我的HTML 5:
...
<body>
<form method="POST" action="http://localhost:3000">
<input id="user" type="text" readonly/>
<label> Test1
<input type="number" name="work" min="0" max="10" required />
</label>
<label> Test2
<input type="number" name="buero" min="0" max="10" required />
</label>
<input type="submit" />
</form>
</body>
...
http.createServer((req, res) => {
...
let fileResult = readFileSync("./Result.html", { encoding: "utf8" })
let body = "";
req.on("data", (d) => {
body += d; // BreakPoint1
})
req.on("readable", function (d) {
body += d; // BreakPoint2
})
req.on("end", () => {
handlePostData(JSON.parse(body))
})
res.writeHead(200, { "Content-Type": "text/html" });
res.write(fileResult)
res.end();
...
我想知道为什么它只会遇到BreakPoint2,我还以为它会遇到BreakPoint1呢。但当它遇到BreakPoint2时,d
就是undefined
。
如果我将method
更改为GET
,req.url将如预期的那样。
发布于 2019-07-17 16:19:57
readable
事件不向回调提供数据。您必须使用this.read()
手动获取它。
https://stackoverflow.com/questions/57080261
复制相似问题