gpt4 book ai didi

javascript - Node.js "write after end"错误

转载 作者:数据小太阳 更新时间:2023-10-29 04:47:24 29 4
gpt4 key购买 nike

从一个基本的 Node 应用程序开始,我无法弄清楚如何解决这个“结束后写入”错误,即使几个网站上建议的回调也是如此。

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

服务器.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;

路由器.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 Server has started. Request for / received. About to route a request for / Request handler 'start' was called. About to route a request for / Request handler 'start' was called. events.js:141 throw er; // Unhandled 'error' event ^

Error: write after end at ServerResponse.OutgoingMessage.write (_http_outgoing.js:430:15) at Object.start (/Users/francop/Documents/Apps/HelloWorld/requestHandlers.js:18:14) at route (/Users/francop/Documents/Apps/HelloWorld/router.js:5:21) at IncomingMessage. (/Users/francop/Documents/Apps/HelloWorld/server.js:19:6) at emitNone (events.js:67:13) at 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)

我在任何地方找到的唯一答案是“它是异步的,所以使用回调”,如下所示:

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

...抛出完全相同的错误。

任何对此的帮助都会很棒。我在网上找到的一些稀疏答案根本没有帮助。

谢谢!

最佳答案

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()时,会将缓冲的header信息和第一个body发送给客户端。第二次 response.write() 被调用时,Node.js 假定您将流式传输数据,并单独发送。也就是说,响应缓冲到正文的第一个 block 。

所以,如果你只想发送一个 block ,你必须使用 response.end(body),如果你想发送多个 block ,你必须使用 response 结束调用.end() 在多次 response.write() 之后。

单 block 示例:

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

示例多个 block :

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

关于javascript - Node.js "write after end"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39261433/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com