write after end -- nodejs response

10443 단어 nodejs
index.js
var server=require("./server");
var route=require("./route");
var requestHandlers=require("./requestHanders");

var handle={};
handle["/"]=requestHandlers.start;
handle['/start']=requestHandlers.start;
handle["/upload"]=requestHandlers.upload;

server.start(route.route,handle);

server.js
var http =require("http");
var url=require("url");


function start(route,handle){
    var onRequest=function(request,response){
        var postData="";
        var pathname=url.parse(request.url).pathname;
        console.log("request for "+pathname+" have received");
        request.setEncoding("utf-8");
        request.addListener("data",function(postDataChunk){
            postData += postDataChunk;
            console.log("receive POST data chunk '"+ postDataChunk + "'.");
        })
        request.addListener("end",function(){
            console.log(postData);
            route(handle,pathname,response,postData);
        })
        route(handle,pathname,response);

    }
    http.createServer(onRequest).listen(3000);
    console.log("Server has started");
}
module.exports.start=start;

route.js
function route(handle,pathname,response,postData){
    console.log("about to route a request for"+ pathname);
    if(typeof handle[pathname]=='function'){
        console.log("route--"+postData);
       handle[pathname](response,postData);
    }else{
        console.log("No request handler found for "+ pathname);
        response.writeHead(404,{"Content-Type":"text/plain"});
        response.write("404 not found");
        response.end();
    }
}

module.exports.route=route;

requestHandles.js
var exec = require("child_process").exec;
var querystring=require("querystring");
function start(response,postData) {
    console.log("request handler 'start' was called");
    var body=''+
        ''+
        '"Content-Type" content="text/html";'+
        'charset=UTF-8" />'+
        ''+
        ''+
        '
" method="
post">'+ '20" cols="60" >'+ '" value="Submit text" />' + ''+ ''+ ''; response.writeHead(200,{ "Content-Type": "text/html"}); response.end(body); } function upload(response,postData){ console.log("request handler 'upload' was called"); console.log("handle-upload"+ postData); response.writeHead(200, { "Content-Type": "text/plain" }); var message="you have sent text "+ querystring.parse(postData).text; console.log(message); response.write(message); response.end(); } module.exports.start=start; module.exports.upload=upload;

브 라 우 저 에서 먼저 접근http://127.0.0.1:3000/start; textarea 입력 이 나타 나 면 123345345323 을 입력 하고 제출 을 누 르 십시오.http://127.0.0.1:3000/upload 정상적으로 되 돌아 가 야 합 니 다. you have sent text 123345345323, 하지만 되 돌아 갑 니 다 you have sent text undefined
또한 콘 솔 에 다음 과 같은 오류 가 발생 했 습 니 다.
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at write_ (_http_outgoing.js:625:15)
    at ServerResponse.write (_http_outgoing.js:620:10)
    at Object.upload [as /upload] (D:\study\js\study01\requestHanders.js:30:14)
    at route (D:\study\js\study01\route.js:5:24)
    at IncomingMessage. (D:\study\js\study01\server.js:17:13)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1056:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

D:\study\js\study01>

해답 을 구하 다Any help with this would be amazing. The few sparse answers I’ve found online don’t help at all.

좋은 웹페이지 즐겨찾기