request.on('data'()) 永远不会被访问
request.on('data'()) never gets accesed
我想在 node.js 中处理来自表单的数据而没有任何框架(没有表达),我看到了这样的解决方案:
if (req.url === "/Login.html" && req.method == 'POST') {
req.on('data', (chunk) => {
console.log(chunk); //never gets fired
}).on('end', () => {
console.log("end"); //this does
});
{
res.writeHead(200, { 'Content-Type': 'text/html' })
fs.readFile("Login.html", (err, data) => {
res.write(data);
res.end();
});
}
}
有Login.html喜欢:
<!DOCTYPE html>
<html>
<head>
<title>ESREP</title>
</head>
<body>
<div class="nav">
<a href="Produse.html">Produse</a>
<a href="Documentation.html">About</a>
<a href="Login.html">Login</a>
</div>
<div class="login">
<h1>Login</h1>
<form method="POST">
<div class="field">
<input type="email" placeholder="Email" required>
</div>
<div class="field">
<input type="password" placeholder="Password" required>
</div>
<button type="submit">Submit</button>
</form>
<div id="sign">
<p>Sign up <a href="Signup.html">here</a></p>
</div>
</div>
</body>
</html>
但似乎 req.on('data',()) 部分从未被访问过。 req.on('end') 总是这样。
您的表单没有成功的字段(name
属性是成功的先决条件)因此没有数据,因此发送 content-length 为零的正文(即没有数据) .
我想在 node.js 中处理来自表单的数据而没有任何框架(没有表达),我看到了这样的解决方案:
if (req.url === "/Login.html" && req.method == 'POST') {
req.on('data', (chunk) => {
console.log(chunk); //never gets fired
}).on('end', () => {
console.log("end"); //this does
});
{
res.writeHead(200, { 'Content-Type': 'text/html' })
fs.readFile("Login.html", (err, data) => {
res.write(data);
res.end();
});
}
}
有Login.html喜欢:
<!DOCTYPE html>
<html>
<head>
<title>ESREP</title>
</head>
<body>
<div class="nav">
<a href="Produse.html">Produse</a>
<a href="Documentation.html">About</a>
<a href="Login.html">Login</a>
</div>
<div class="login">
<h1>Login</h1>
<form method="POST">
<div class="field">
<input type="email" placeholder="Email" required>
</div>
<div class="field">
<input type="password" placeholder="Password" required>
</div>
<button type="submit">Submit</button>
</form>
<div id="sign">
<p>Sign up <a href="Signup.html">here</a></p>
</div>
</div>
</body>
</html>
但似乎 req.on('data',()) 部分从未被访问过。 req.on('end') 总是这样。
您的表单没有成功的字段(name
属性是成功的先决条件)因此没有数据,因此发送 content-length 为零的正文(即没有数据) .