使用命令行执行NODE.js脚本无反应
No response when using commandline to execute NODE.js script
我正在尝试编写一个脚本,用户可以在其中访问本地网站并粘贴他们的加密文本。但是,当我执行以下代码时,当用户提交文本时没有显示任何响应。我正在尝试使用 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")
我看到你的代码有两个错误,你需要修改。
首先是<form method='post'><form>
删除表格的一个标签
其次是 <input type='text' id='encryptedtext' value='Paste encrypted text.'/>
你不需要 'id' 但 'name' .
下面有完整的修复
response.write("<html><body><form method='post'><tr><td>Enter the
encrypted text:</td><td><input type='text' name='encryptedtext' value='Paste
encrypted text.'/></td></tr><tr><td><input type='submit' value='Send'/></td>
</tr></form> </body></html>")
};
您的代码有几个问题:
- 在函数
getSendForm()
中,缺少response.end()
,这意味着网页/send
永远不会结束加载——页面中会有一个旋转器保持运行选项卡
- 错误地插入了
<form>
标签,如@TonyY 所述。
- 表单的
action
不见了,根据你的代码应该是/send
name
of input
元素缺失,如@TonyY 所述。
Paste encrypted text.
应该是 placeholder
,而不是 value
。
response.end()
在 POST /send
的处理程序中缺失,因此在提交表单后没有向用户显示任何响应。
固定代码如下:
const http = require('http')
var qs = require("querystring");
const port = 9000
...
function getSendForm(request, response) {
response.write("<html><body><form method='post' action='/send'><tr><td>Enter the encrypted text:</td><td><input type='text' id='encryptedtext' name='encryptedtext' placeholder='Paste encrypted text.'/></td></tr><tr><td><input type='submit' value='Send'/></td></tr></form> </body></html>")
response.end();
};
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);
response.end('Done!');
});
}
else {
get404(request, response);
}
break;
default:
get405(request, response);
break;
}
}).listen(port);
console.log("server")
我正在尝试编写一个脚本,用户可以在其中访问本地网站并粘贴他们的加密文本。但是,当我执行以下代码时,当用户提交文本时没有显示任何响应。我正在尝试使用 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")
我看到你的代码有两个错误,你需要修改。
首先是<form method='post'><form>
删除表格的一个标签
其次是 <input type='text' id='encryptedtext' value='Paste encrypted text.'/>
你不需要 'id' 但 'name' .
下面有完整的修复
response.write("<html><body><form method='post'><tr><td>Enter the
encrypted text:</td><td><input type='text' name='encryptedtext' value='Paste
encrypted text.'/></td></tr><tr><td><input type='submit' value='Send'/></td>
</tr></form> </body></html>")
};
您的代码有几个问题:
- 在函数
getSendForm()
中,缺少response.end()
,这意味着网页/send
永远不会结束加载——页面中会有一个旋转器保持运行选项卡 - 错误地插入了
<form>
标签,如@TonyY 所述。 - 表单的
action
不见了,根据你的代码应该是/send
name
ofinput
元素缺失,如@TonyY 所述。Paste encrypted text.
应该是placeholder
,而不是value
。response.end()
在POST /send
的处理程序中缺失,因此在提交表单后没有向用户显示任何响应。
固定代码如下:
const http = require('http')
var qs = require("querystring");
const port = 9000
...
function getSendForm(request, response) {
response.write("<html><body><form method='post' action='/send'><tr><td>Enter the encrypted text:</td><td><input type='text' id='encryptedtext' name='encryptedtext' placeholder='Paste encrypted text.'/></td></tr><tr><td><input type='submit' value='Send'/></td></tr></form> </body></html>")
response.end();
};
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);
response.end('Done!');
});
}
else {
get404(request, response);
}
break;
default:
get405(request, response);
break;
}
}).listen(port);
console.log("server")