Node.js"写完后#34;错误

时间:2016-09-01 01:32:45

标签: javascript node.js node.js-stream

从一个基本的节点应用开始,我无法弄清楚如何击败这个"写完后#34;错误,即使在几个站点上建议回调。

index.js:

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");

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

server.start(router.route, handle);

server.js:

var http = require("http");
var url = require("url");

function start(route, handle) {
  function onRequest(request, response) {
    var postData ="";
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");

    route(handle, pathname, response);
   }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}

exports.start = start;

router.js:

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

exports.route = route;

and now the culprit, requestHandlers.js:

function start(response) {
  console.log("Request handler 'start' was called.");

  var body = '<html>'+
    '<head>'+
    '<meta http-equiv="Content-Type" content="text/html; '+
    'charset=UTF-8" />'+
    '</head>'+
    '<body>'+
    '<div>Hello Start</div>' +
    '</body>'+
    '</html>';

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();
}

function upload(response) {
    console.log("Request handler 'upload' was called");
    response.writeHead(200, {"Content-Type": "text/plain"}); 
    response.write("Hello Upload");
    response.end();
}

exports.start = start;
exports.upload = upload;

这是堆栈跟踪:

  

C02RF8L7FVH5:HelloWorld francop $ node index.js   服务器已启动。   请求/收到。   即将发送请求/   请求处理程序&#39; start&#39;被称为。   即将发送请求/   请求处理程序&#39; start&#39;被称为。   events.js:141         扔掉//未处理的错误&#39;事件         ^

     

错误:写完后       在ServerResponse.OutgoingMessage.write(_http_outgoing.js:430:15)       在Object.start(/Users/francop/Documents/Apps/HelloWorld/requestHandlers.js:18:14)       在路线(/Users/francop/Documents/Apps/HelloWorld/router.js:5:21)       在IncomingMessage。 (/Users/francop/Documents/Apps/HelloWorld/server.js:19:6)       在emitNone(events.js:67:13)       在IncomingMessage.emit(events.js:166:7)       at endReadableNT(_stream_readable.js:921:12)       at nextTickCallbackWith2Args(node.js:442:9)       at process._tickCallback(node.js:356:17)

我在任何地方找到的唯一答案是&#34;它是异步的所以请使用回调&#34;像这样:

response.write(body, function(err) {
    response.end();
});

...这会引发完全相同的错误。

任何帮助都会令人惊讶。我在网上发现的一些稀疏答案根本没有帮助。

谢谢!

2 个答案:

答案 0 :(得分:2)

response.end()代替response.write()

返回您的遗体
function start (response) {
    console.log("Request handler 'start' was called.");
    var body = '<html>'+
    '<head>'+
    '<meta http-equiv="Content-Type" content="text/html; '+
    'charset=UTF-8" />'+
    '</head>'+
    '<body>'+
    '<div>Hello Start</div>' +
    '</body>'+
    '</html>';

    response.writeHead(200, {"Content-Type": "text/html"});
    response.end(body);
}

编辑1:

第一次调用response.write()时,它会将缓冲的头信息和第一个主体发送到客户端。第二次调用response.write()时,Node.js假定您将成为流数据,并单独发送。也就是说,响应被缓冲到第一个身体块。

因此,如果您只想发送一个块,则必须使用response.end(body),如果要发送多个块,则必须在多个response.end()之后使用response.write()结束调用

示例单个块:

function (chunk){
    response.end(chunk);
}

示例多个块:

function (chunkArray){
    // loop on chunks 
    for(var i=0; i < chunkArray.length; i++){
        response.write(chunkArray[i]);                
    }

    // end the response with empty params
    response.end();
}

答案 1 :(得分:1)

这是来自&#34; Node Beginner&#34;

的示例

我已经重新发布了以下文件,因为截至本文发布时,您的帖子与路由器之间没有分离。\ n \ n \ n&!路由器。\ n&#34;和&#34; requestHanders.js&#34;。

我在节点v8中运行此问题时遇到了同样的问题,当前运行时没有事件节点v9.5.0。我不是把它作为问题的解决方案,只是说它对我有用。也许这是一个在更新版本中解决的问题,我无法肯定地说。

您的错误流也会读取&#34; requestHandlers.js:18:14&#34;如果您确认代码中的第18行(将来),您可能会得到更好的结果。没有空格第18行是声明上传功能之前的行。事实上,这个错误是由&#34; response.end();&#34;

触发的

index.js

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");

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

server.start(router.route, handle);

router.js

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

exports.route = route;

server.js

var http = require("http");
var url = require("url");

function start(route, handle) {
  function onRequest(request, response) {
    var postData ="";
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");

    route(handle, pathname, response);
   }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}

exports.start = start;

requestHandlers.js

function start(response) {
  console.log("Request handler 'start' was called.");

  var body = '<html>'+
    '<head>'+
    '<meta http-equiv="Content-Type" content="text/html; '+
    'charset=UTF-8" />'+
    '</head>'+
    '<body>'+
    '<div>Hello Start</div>' +
    '</body>'+
    '</html>';

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();
}

function upload(response) {
    console.log("Request handler 'upload' was called");
    response.writeHead(200, {"Content-Type": "text/plain"}); 
    response.write("Hello Upload");
    response.end();
}

exports.start = start;
exports.upload = upload;