我正在尝试以表格形式发送数据(用户名和密码)。我想在服务器上检查用户名和密码是否正确。让我们假设我们想检查是否 uName
== John, and pwd
== 1234。
在下面的代码中,我得到输出:uName=john&pwd=1234
if(req.method == "POST") {
让身体 = '';
req.on('数据', res => {
body += res.toString(); // 将缓冲区转换为字符串
打印(资源);
});
}
有没有一种方法可以单独获得 uName
和 pwd
,而不必使用 string.split
和 substring
等?例如,如果我可以做类似 if (res[uName] == "John)"
完整代码如下:
var http = require('http');
var styleSheet="<link rel='stylesheet' href='css/site.css'>\n";
var server = http.createServer(function(req, resp) {
//console.log("Req: "+ req.url+"\nResponse: "+resp);
if(req.method == "POST") {
//print("Method: "+req.method);
let body = '';
req.on('data', res => {
body += res.toString();
print(body);
});
}
if(req.url == "/") {
resp.writeHead(200, { 'Content-Type': 'text/html'});
var html = "<!DOCTYPE html>\n<html>"+
"<head>"+
"<link rel='stylesheet' type='text/css' href='css/site.css'>\n"+
"<title>Webproj</title>"+
"<script src='scripts/jquery-3.5.1.min.js'></script>\n"+
"</head>"+
"<body>" +
"<div id='cDiv'>"+
"<h2> Welcome.</h2>"+
"<form action='home' method='post'>"+
"<p><input type='text' id='uName' name='uName' /></p>"+
"<p><input type='password' id='pwd' name='pwd' /></p>"+
"<p><input type='submit' value='Login'>"+
"</form>"+
"</div>"+
"</body>"+
"</html>";
resp.write(html);
resp.end();
return;
}
if(req.url == "/home") {
resp.writeHead(200, { 'Content-Type': 'text/html'});
resp.write(home());
resp.end();
return;
}
else {
resp.end("Error");
}
});
server.listen(1000);
console.log("Server is listening on port 1000");
我没有写任何客户端代码,客户端只是浏览器使用:localhost:1230