![](/img/trans.png)
[英]node.js ~ express req.on('end') triggers but not req.on('data')
[英]Why do control is not going inside req.on(“data”, data => { }); I'm trying to handle POST method data
** 我正在嘗試獲取 POST 方法提交的數據,但我無法獲取數據,因為在 req.on("data", data=> {}) 內部沒有更新 body 變量。**
const {createServer} = require("http");
const {createReadStream} = require("fs");
const {decode} = require("querystring");
const sendfile = (res, status, type, filepath) => {
res.writeHead(status, {"Content-Type" : type});
createReadStream(filepath).pipe(res); //readable stream work with writable stream
};
createServer((req, res) =>{
if(req.method === 'POST') {
let body = "";
req.on("data", data => {
body+=data;
});
req.on("end", () => {
const {name, email, message} = decode(body);
console.log(`Name : ${name}`);
console.log(`Email : ${email}`);
console.log(`Message : ${message}`);
});
}
switch(req.url) {
case "/" : return sendfile(res, 200, "text/html", "./home_page.html");
case "/message" : return sendfile(res, 200, "text/html", "./forms.html")
default : return sendfile(res, 200, "text/html", "./404.html");
}
}).listen(8000);
應該:
req.on("data", (data)=> {})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.