使用命令行执行NODE.js脚本时无响应

时间:2017-04-21 01:28:44

标签: node.js http console request response

我正在尝试创建一个脚本,用户可以访问本地网站并粘贴其加密文本。但是,当我执行以下代码时,在用户提交文本时没有响应。我试图使用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")

2 个答案:

答案 0 :(得分:0)

我看到你的代码有两个错误,你需要修复它。

首先<form method='post'><form>删除表单

的一个标记

第二个是<input type='text' id='encryptedtext' value='Paste encrypted text.'/>您不需要&#39; id&#39;但是&#39; name&#39;

完全修复如下:

 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>")  
    };

答案 1 :(得分:0)

您的代码存在一些问题:

  1. 在功能getSendForm()中,response.end()缺失,这意味着网页/send永远不会结束加载 - 在页面标签中会有一个微调器继续运行。
  2. 错误地插入<form>标记,如@TonyY所述。
  3. 缺少action表单,根据您的代码,该/send应为name
  4. {li> input Paste encrypted text.元素缺失,正如@TonyY所提到的那样。
  5. placeholder应该是value,而不是response.end()
  6. {li> POST /sendconst 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") 的处理程序中缺失,因此在提交表单后没有向用户显示任何响应。

    这是固定代码:

    d28a36113a206f9f84cc97bc72ff84aa93f87ee8