错误'仅在Cloud9上发送后才能设置标头'

时间:2013-09-13 08:37:07

标签: node.js express socket.io cloud9-ide

在Cloud9 ide上同时使用Express和socket.io时出现以下错误:

http.js:707
    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:707:11)
    at ServerResponse.res.setHeader (/var/lib/stickshift/523075494382ec51c3000001/app-root/data/610464/node_modules/express/node_modules/connect/lib/patch.js:59:22)
    at next (/var/lib/stickshift/523075494382ec51c3000001/app-root/data/610464/node_modules/express/node_modules/connect/lib/proto.js:153:13)
    at Function.app.handle (/var/lib/stickshift/523075494382ec51c3000001/app-root/data/610464/node_modules/express/node_modules/connect/lib/proto.js:198:3)
    at Server.app (/var/lib/stickshift/523075494382ec51c3000001/app-root/data/610464/node_modules/express/node_modules/connect/lib/connect.js:65:37)
    at Manager.handleRequest (/var/lib/stickshift/523075494382ec51c3000001/app-root/data/610464/node_modules/socket.io/lib/manager.js:564:28)
    at Server. (/var/lib/stickshift/523075494382ec51c3000001/app-root/data/610464/node_modules/socket.io/lib/manager.js:118:10)
    at Server.EventEmitter.emit (events.js:117:20)
    at HTTPParser.parser.onIncoming (http.js:2056:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:120:23)

以下是我的server.js代码:

var express = require('express');
var app = express()
  , http = require('http')
  , server = http.createServer(app)
  , io = require('socket.io').listen(server);


server.listen(process.env.PORT, process.env.IP);

app.get('/', function(req,res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('Hello World\n');
})

我正在使用节点:“0.10.x”,socket.io:“0.9.14”并表示:“3.x”。此错误仅在Cloud9 ide上出现。在localhost上运行正常。 请帮忙。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。虽然这不是一个好的解决方案,但将Express恢复到3.1.0版本可以解决这个问题。最高版本的Express和Socket.io不能很好地协同工作。

我在恢复后使用的版本是:

节点: 0.10.x

Socket.io: 0.9.16

快递: 3.1.0