gpt4 book ai didi

javascript - Express 4错误: Can't set headers after they are sent

转载 作者:行者123 更新时间:2023-12-03 10:25:13 25 4
gpt4 key购买 nike

我正在尝试使用 Express 建立一个简单的聊天系统。我刚刚从 v.3 迁移到 v.4,现在收到此错误消息:

   info  - socket.io started
Express server listening on port 3000
GET / 304 790.443 ms - -
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:707:11)
at ServerResponse.res.setHeader (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/connect/lib/patch.js:134:22)
at Object.expressInit [as handle] (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/lib/middleware.js:20:42)
at next (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/connect/lib/proto.js:174:15)
at Object.query [as handle] (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/connect/lib/middleware/query.js:43:5)
at next (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/connect/lib/proto.js:174:15)
at Function.app.handle (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/connect/lib/proto.js:182:3)
at Server.app (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/connect/lib/connect.js:67:37)
at Manager.handleRequest (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/socket.io/lib/manager.js:564:28)
at Server.<anonymous> (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/socket.io/lib/manager.js:118:10)
GET /javascripts/jquery.min.js 304 7.874 ms - -
GET /javascripts/jquery.min.js 304 7.966 ms - -
GET /javascripts/chat.js 304 4.132 ms - -
GET /javascripts/chat.js 304 4.213 ms - -
GET /socket.io/socket.io.js 404 11.137 ms - 35
GET /stylesheets/style.css 304 1.715 ms - -
GET /stylesheets/style.css 304 1.798 ms - -
Error: Can't set headers after they are sent.
at SendStream.headersAlreadySent (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/send/index.js:319:13)
at SendStream.send (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/send/index.js:501:17)
at onstat (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/send/index.js:600:10)
at Object.oncomplete (fs.js:107:15)
Error: Can't set headers after they are sent.
at SendStream.headersAlreadySent (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/send/index.js:319:13)
at SendStream.send (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/send/index.js:501:17)
at onstat (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/send/index.js:600:10)
at Object.oncomplete (fs.js:107:15)
Error: Can't set headers after they are sent.
at SendStream.headersAlreadySent (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/send/index.js:319:13)
at SendStream.send (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/send/index.js:501:17)
at onstat (/Users/bruger/Google Drev/Uni/8. semester/Real-Time-Webapplikationer/0786OS_Code/Chapter 04/section02/awesome-chat/node_modules/express/node_modules/send/index.js:600:10)
at Object.oncomplete (fs.js:107:15)
debug - served static content /socket.io.js

当我第一次启动服务器实例时,一切看起来都很好,直到我进入浏览器,然后出现错误。

我一直在尝试解决这个问题,阅读了几乎所有有关的帖子

Error: Can't set headers after they are sent.

但是我不知道我做错了什么?这是我设置 app.js 文件的代码:

var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path');

var methodOverride = require('method-override')
var json = require('json-middleware');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();

var app = express();

// Express 4 version
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
// configure stuff here
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(json);
app.use('/public/javascripts/chat.js', multipartMiddleware);
//app.use(express.bodyParser());
//app.use(express.methodOverride());
app.use(methodOverride('_method'))
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
}


app.get('/', routes.index);

var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});

//routes.sockets.connect(server);
require('./routes/sockets.js').initialize(server);

可能是我的index.js或socket.js文件中生成了错误,所以我也添加了这些。索引.js:

exports.index = function(req, res){
res.render('index', { title: 'Express Chat' });
};

sockets.js:

var io = require('socket.io');

exports.initialize = function(server) {
io = io.listen(server);
io.sockets.on("connection", function(socket){
socket.on('message', function(message){
message= JSON.parse(message);
if(message.type == "userMessage"){
socket.get('nickname', function(err, nickname){
message.username=nickname;
socket.broadcast.send(JSON.stringify(message));
message.type = "myMessage";
socket.send(JSON.stringify(message));
});
}
});

socket.on("set_name", function(data){
socket.set('nickname', data.name, function(){
socket.emit('name_set', data);
socket.send(JSON.stringify({type:'serverMessage', message: 'Welcome to the most interesting chat room on earth!'}));
});
});
});
}

我是 Express 和 Node.js 的新手,两天来我一直在努力理解它,因此非常感谢所有帮助!

最佳答案

这种错误通常源于未返回您的发送函数,然后执行中稍后的某些操作也发送数据。

为了缓解这种情况,您应该始终返回发送。

  return socket.send(JSON.stringify(message));

这将确保不会设置多个 header 。

关于javascript - Express 4错误: Can't set headers after they are sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29434991/

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