Using socket.io in Express 4 and express-generator's /bin/www

問題:

So here is the deal: I'm trying to use socket.io in an express project.所以這就是交易:我正在嘗試在一個快速項目中使用 socket.io。 After Express Js 4 was lauched, i've updated my express-generator and now the app initial functions goes into ./bin/www file, including those vars (www file contents: http://jsfiddle.net/avMa5/ ) Express Js 4 發佈後,我更新了我的 express-generator,現在應用程序初始功能進入./bin/www文件,包括那些變量(www 文件內容: http : //jsfiddle.net/avMa5/

var server = app.listen(app.get('port'), function() {..}

(check it by npm install -g express-generator and then express myApp (通過npm install -g express-generator ,然後express myApp

that being said, let's remember how socket.io docs ask us to fire it:話雖如此,讓我們記住 socket.io 文檔如何要求我們觸發它:

var app = require('express').createServer();
var io = require('socket.io')(app);

Ok but i can't do it inside app.js, like recommended.好的,但我不能在 app.js 中做到這一點,就像推薦的那樣。 This should be done in ./bin/www in order to work.這應該在 ./bin/www 中完成才能工作。 in ./bin/www this is what i can do to get it working:在 ./bin/www 這是我可以做的讓它工作:

var io = require('socket.io')(server)

Ok this works, but i can't use the io var anywhere else, and i really don't want to put my socket.io functions on www file.好的,這可行,但我不能在其他任何地方使用 io var,而且我真的不想將 socket.io 函數放在www文件中。

I guess this is just basic syntax, but I can't get this to work, not even using module.exports = server or server.exports = server nor module.exports.io = app(io) on www file我想這只是基本語法,但我無法module.exports = server工作,甚至不能在 www 文件上使用module.exports = serverserver.exports = servermodule.exports.io = app(io)

So the question is: how can i use socket.io having this /bin/www file as starting point of my app?所以問題是:我如何使用 socket.io 將此 /bin/www 文件作爲我的應用程序的起點?


解決方案:

參考: https://stackoom.com/en/question/1fGBL
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章