2
votes

I am getting following error while using Express and socket.io together on Cloud9 ide:

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)

Below is my server.js code:

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

I am using node: "0.10.x", socket.io: "0.9.14" and express: "3.x". This error is coming only on Cloud9 ide. On localhost it runs fine. Please help.

1
It works fine for me: c9.io/fardjad/node_version_testfardjad
This happens when try to write into or modify header of a response after it has been sent.user568109
@fardjad, I have the same code as you in server.js and same package.json file, but I am still getting this error. What node version you are using? When i try to access the app in brower, I get the output 'Hello World', but after that the error occurs which you can see in the Output console of cloud9 ide.Mahesh
process.version in my c9 workspace is v0.10.15.fardjad

1 Answers

0
votes

I ran into this exact same problem. Although it's not a good solution, reverting Express back to version 3.1.0 solved this for me. It appears at a high level that the latest versions of Express and Socket.io don't play nicely together.

The versions I am using after reverting are:

Node: 0.10.x,

Socket.io: 0.9.16,

Express: 3.1.0