node.js + express 3 + socket.io = Can't set headers after they are sent

浪子不回头ぞ 提交于 2019-12-01 04:09:45

What version of Node are you using?

I had the same problem when I was using 0.9.x. I downgraded Node to 0.8.4 and the problem seems to have gone away.

My best guess is something in Node has changed that Socket.io doesnt agree with.

upgrade express - 3.1.0 and socket.io - 0.9.13

it's ok on nodejs0.10

Which version of express are you using? Check out https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x or try this:

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

i was having the same issue mate

if you are using express 3.0 you should try this: 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 helped me! And will help you

For anyone bumping in to this issue you should be using:

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

As express no longer returns a HTTP instance that socket.io requires. See the updated README.md file on https://github.com/learnboost/socket.io for the express 2 and 3 examples.

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

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

Or you can downgrade your socket.io to a previous version, but you'll have to figure that one out.

Sunil More

this error come because express internally use cache means for every second request data get from cache so it response back with 304 status code so use

app.disable('etag');

it tells the express each request fresh means with stautscode 200

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

Run a npm list socket.io

Make sure it doesn't show invalid as below,

─ 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!