【发布时间】:2017-04-21 01:28:43
【问题描述】:
我正在尝试编写一个脚本,用户可以在其中访问本地网站并粘贴他们的加密文本。但是,当我执行以下代码时,当用户提交文本时没有显示响应。我正在尝试使用 console.log(reqbody) 显示输入的文本,但没有显示任何内容。非常感谢您的帮助!
const http = require('http')
var qs = require("querystring");
const port = 9000
function getHome(request, response) {
response.writeHead(200, { "Content-Type": "text/html" });
response.write("<html><body>Encrypt your email! <a
href='http://travistidwell.com/jsencrypt/demo/index.html'>Click.</a> Verify
Wi-Fi AP <a href='/send'>Click here.</a></body ></html > ")
response.end();
}
function get404(request, response) {
response.writeHead(404, "Resource Not Found", { "Content-Type":
"text/html" });
response.write("<html><title> Error 404 </title><h1>404: Resource not
found!</h1></html >")
response.end();
}
function get405(request, response) {
response.writeHead(405, "Method not supported", { "Content-Type":
"text/html" });
response.write("<html><title> Error 405</title><h1>Error 405: Method not
supported.</h1></html >")
response.end();
}
function getSendForm(request, response) {
response.write("<html><body><form method='post'><form><tr><td>Enter the
encrypted text:</td><td><input type='text' id='encryptedtext' value='Paste
encrypted text.'/></td></tr><tr><td><input type='submit' value='Send'/></td>
</tr></form> </body></html>")
};
http.createServer(function (request, response) {
console.log(request.url);
switch (request.method) {
case "GET":
if (request.url === "/") {
getHome(request, response);
}
else if (request.url === "/send") {
getSendForm(request, response);
}
else {
get404(request, response);
}
break;
case "POST":
if (request.url === "/send") {
var reqBody = '';
request.on('data', function (data) {
reqBody += data;
if (reqBody.length > 1e7) //10mb
{
response.writeHead(413, 'Too large encrypted
data.', { 'Content-Type': 'text/html' });
response.write("<html><title> Error 413 </title>
<h1>413: Too much data.</h1></html >")
response.end();
}
});
request.on('end', function (data) {
console.log(reqBody);
});
}
else {
get404(request, response);
}
break;
default:
get405(request, response);
break;
}
}).listen(port);
console.log("server")
【问题讨论】:
标签: node.js http console request response