Node.js + express 3 + socket.io = Unable to set headers after sending them

I am new to learning node.js and seem to be facing an error that cannot be fixed.

Its code is very simple and new, so it doesn’t need a lot of explanation, moreover, it works fine on localhost, but it crashes into a production server.

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

And here is a terrible mistake!

 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) 

The problem seems to be in var io = require('socket.io').listen(server); , because commenting of this type eliminates the error.

+7
source share
10 answers

What version of Node are you using?

I had the same problem when I used 0.9.x. I downgraded Node to 0.8.4 and the problem seems to be gone.

My best guess is that Node has changed that Socket.io disagrees with.

+7
source

Update express - 3.1.0 and socket.io - 0.9.13

this is normal on nodejs0.10

+4
source

What version of express do you use? Check out https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x or try the following:

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

I had the same pairing

if you are using express 3.0, you should try the following: http://codehenge.net/blog/2012/08/using-socket-io-with-express-3-x/

also check your version of node:

 node --version 

try using v0.8.9 or any other stable version

It helps me! And help you

+2
source

For those facing this problem, you should use:

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

As an expression, it no longer returns the HTTP instance that socket.io requires. See the Updated README.md file at https://github.com/learnboost/socket.io for express examples 2 and 3.

+1
source

This approach works in the current stable release 0.8.14:

 var express = require('express') , app = express() , server = app.listen(8000) , io = require('socket.io').listen(server); 
+1
source

I had the same problem. Incompatible versions of express and socket.io are to blame. Upgraded them to express 3.2.4 and socket.io 0.9.14 and works like a charm.

Or you can change your .io socket to a previous version, but you will have to display this.

0
source

this error occurs because the expression internally uses caching for every second request for data from the cache, so it returns a response with a status code of 304, so use

 app.disable('etag'); 

it tells express to every request fresh means using stautscode 200

0
source

Change socket.io dependency to 0.9.15 in package.json, run npm install and it should fix your problem.

0
source

Run npm list socket.io

Make sure that it does not appear as invalid.

─ socket.io@0.9.10 invalid

npm ERR! invalid: socket.io@0.9.10 c: \ Users ....

If you get this error, npm install socket.io. Also make sure the same with other packages.

0
source

All Articles