I want to process data from a form in node.js without any framework (no express) and i saw a solution like this:
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();
});
}
}
having Login.html like:
<!DOCTYPE html>
<html>
<head>
<title>ESREP</title>
</head>
<body>
<div >
<a href="Produse.html">Produse</a>
<a href="Documentation.html">About</a>
<a href="Login.html">Login</a>
</div>
<div >
<h1>Login</h1>
<form method="POST">
<div >
<input type="email" placeholder="Email" required>
</div>
<div >
<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>
but it seems that the req.on('data',()) part never gets accesed. The req.on('end') always does.
CodePudding user response:
Your form has no successful fields (name
attributes are a prerequisite to being successful) so has no data, so sends a body with a content-length of zero (i.e. there is no data).