StringBuilder结束后在node.js中写入错误

时间:2018-06-27 05:40:22

标签: node.js stringbuilder

 C:\Users\admin>httpsample.js
    /calc
    events.js:167
          throw er; // Unhandled 'error' event
          ^

    Error [ERR_STREAM_WRITE_AFTER_END]: write after end
        at write_ (_http_outgoing.js:580:17)
        at ServerResponse.write (_http_outgoing.js:575:10)
        at C:\Users\admin\httpsample.js:21:8
        at C:\Users\admin\node_modules\stringbuilder\lib\stringbuilder.js:212:4
        at C:\Users\admin\node_modules\stringbuilder\lib\stringbuilder.js:192:7
        at C:\Users\admin\node_modules\stringbuilder\lib\stringbuilder.js:351:4
        at C:\Users\admin\node_modules\async\lib\async.js:232:13
        at C:\Users\admin\node_modules\async\lib\async.js:119:25
        at C:\Users\admin\node_modules\async\lib\async.js:24:16
        at C:\Users\admin\node_modules\async\lib\async.js:229:17
    Emitted 'error' event at:
        at writeAfterEndNT (_http_outgoing.js:639:7)
        at process._tickCallback (internal/process/next_tick.js:63:19)

加载 localhost/calc 时,出现上述错误。下面给出的是代码。我什至试图删除 res.end(),但是没有用。

如果有人可以协助编写此代码,那将是很棒的。其他职位的解决方案对此代码无效。

该错误驻留在执行字符串生成器的代码结果部分中。

var http = require("http");
var qs = require("querystring");
var StringBuilder = require("stringbuilder");

var port = 9000;

function getHome(req,resp){
    resp.writeHead(200,{"Content-Type":"text/html"});
    resp.write("<html><head><title>Home</title></head><body>Need to add some values? Click <a href ='/calc'>here</a></body></html>");
    resp.end();
}
function getCalcForm(req,resp){
    var sb = new StringBuilder({ newline: '\r\n'});

    sb.appendLine("<html>");
    sb.appendLine("<body>");
    sb.appendLine(" <form method='post'>");
    sb.appendLine(" <table>");
    sb.appendLine("     <tr>");
    sb.appendLine("         <td>Enter the FIRST Value</td>");
    sb.appendLine("         <td><input type='text' id='txtFirstNo' value=''/></td>");
    sb.appendLine("     </tr>");
    sb.appendLine("     <tr>");
    sb.appendLine("         <td>Enter the SECOND Value</td>");
    sb.appendLine("         <td><input type='text' id='txtSecondNo' value=''/></td>");
    sb.appendLine("     </tr>");
    sb.appendLine("     <tr>");
    sb.appendLine("         <td><input type='submit' value='Calculate'/></td>");
    sb.appendLine("     </tr>");
    sb.appendLine("     <tr>");
    sb.appendLine("         <td><span>Sum=</span></td>");
    sb.appendLine("     </tr>");
    sb.appendLine(" </table>");
    sb.appendLine(" </form>");
    sb.appendLine("</body>");
    sb.appendLine("</html>");

    sb.build(function(err,result){
        resp.writeHead(200,{"Content-Type":"text/html"});
        resp.write(result);
        resp.end();
    });
}
function get404(req,resp){
    resp.writeHead(404,"Resource not Found",{"Content-Type":"text/html"});
    resp.write("<html><head><title>404</title></head><body>404 Resource not found Click <a href ='/'>home</a></body></html>");
    resp.end();
}       
function get405(req,resp){
    resp.writeHead(405,"Method Not Supported",{"Content-Type":"text/html"});
    resp.write("<html><head><title>405</title></head><body>405 Method Not Found Click <a href ='/'>home</a></body></html>");
    resp.end();
}   
http.createServer(function(req, resp){
    console.log(req.url);
    switch (req.method){
        case "GET":
            if(req.url==="/"){
                getHome(req,resp);
                resp.end();
            }
            else if (req.url==="/calc"){
                getCalcForm(req,resp);
                resp.end();
            }
            else{
                get404(req,resp);
                resp.end();
            }   
            break;
        case"POST":
            break;
        default:{
            get405(req,resp);
        }
            break;
    }
    resp.end();
}).listen(port);

0 个答案:

没有答案