【问题标题】:No response when using commandline to execute NODE.js script使用命令行执行NODE.js脚本时没有响应
【发布时间】: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


    【解决方案1】:

    你的代码有几个问题:

    1. 在函数getSendForm() 中,缺少response.end(),这意味着网页/send 永远不会结束加载——页面标签中会一直运行一个微调器。
    2. @TonyY 提到的 &lt;form&gt; 标签插入不正确。
    3. 表单的action 丢失,根据您的代码应该是/send
    4. @TonyY 提到的 input 元素中的 name 缺失。
    5. Paste encrypted text. 应该是 placeholder,而不是 value
    6. POST /send 的处理程序中缺少 response.end(),因此在提交表单后不会向用户显示任何响应。

    这里是固定代码:

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

    【讨论】:

    • 非常感谢!它工作得很好!我会将它与我创建的 python 脚本一起使用,它应该可以完美运行!
    • 我还有一个问题 xD。输入 !, +, -, = 是在网络服务器上输出不同的文本作为响应。输入示例:测试!正在输出 test%21 你能帮忙吗?谢谢。
    • @user3108169 您可以使用decodeURI() 来获取真正的字符串字符。例如,node.js 中的decodeURI('%21') 将返回'!'
    • 谢谢,但是 decodeURIComponent(data) 效果更好 =)
    【解决方案2】:

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

    首先是&lt;form method='post'&gt;&lt;form&gt;删除form的一个标签

    其次是&lt;input type='text' id='encryptedtext' value='Paste encrypted text.'/&gt;,你不需要'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>")  
        };
    

    【讨论】:

    • 如果我的回答对你有用,请点击复选框接受它作为帮助大家快速找到的答案:>
    猜你喜欢
    • 2019-03-15
    • 2011-10-22
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 2012-10-20
    • 2015-06-17
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多