gpt4 book ai didi

node.js + express 3 + socket.io = 发送后无法设置 header

转载 作者:搜寻专家 更新时间:2023-10-31 22:21:54 24 4
gpt4 key购买 nike

我刚开始学习 node.js,似乎遇到了无法修复的错误。

它是一个非常简单的初学者代码,因此不需要太多解释,而且它在本地主机上运行良好,但在生产服务器上会中断。

App.js

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

var app = express();

app.configure(function(){
app.set('port', process.env.PORT || 8000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
app.use(express.errorHandler());
});

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

var server = app.listen(8000);
var io = require('socket.io').listen(server);

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

这是可怕的错误!

http.js:644
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:644:11)
at ServerResponse.res.setHeader (/home1/artalatc/public_html/cloud1/node_modules/express/node_modules/connect/lib/patch.js:59:22)
at next (/home1/artalatc/public_html/cloud1/node_modules/express/node_modules/connect/lib/proto.js:153:13)
at Function.app.handle (/home1/artalatc/public_html/cloud1/node_modules/express/node_modules/connect/lib/proto.js:198:3)
at Server.app (/home1/artalatc/public_html/cloud1/node_modules/express/node_modules/connect/lib/connect.js:66:31)
at Manager.handleRequest (/home1/artalatc/public_html/cloud1/node_modules/socket.io/lib/manager.js:564:28)
at Server.<anonymous> (/home1/artalatc/public_html/cloud1/node_modules/socket.io/lib/manager.js:118:10)
at Server.EventEmitter.emit (events.js:115:20)
at HTTPParser.parser.onIncoming (http.js:1793:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)

问题似乎出在 var io = require('socket.io').listen(server); 上,因为像这样评论可以消除错误。

最佳答案

您使用的是什么版本的 Node?

我在使用 0.9.x 时遇到了同样的问题。我将 Node 降级到 0.8.4,问题似乎已经消失。

我最好的猜测是 Node 中发生了一些 Socket.io 不同意的更改。

关于node.js + express 3 + socket.io = 发送后无法设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11801882/

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