user1576978 user1576978 - 11 months ago 266
Node.js Question

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

So here is the deal: I'm trying to use in an express project. After Express Js 4 was lauched, i've updated my express-genrator and now the app initial functions goes into

file, including those vars (www file contents: )

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

(check it by
npm install -g express-generator
and then
express myApp

that being said, let's remember how docs ask us to fire it:

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

Ok but i can't do it inside app.js, like recommended. This should be done in ./bin/www in order to work. in ./bin/www this is what i can do to get it working:

var io = require('')(server)

Ok this works, but i can't use the io var anywhere else, and i really don't want to put my functions on

I guess this is just basic sintax, but i can't get this to work, not even using
module.exports = server
server.exports = server
nor = app(io)
on www file

So the question is: how can i use havin this /bin/www file as starting point of my app?


It turns out it really was some basic sintax problem.... I got these lines from this chat tutorial...

on ./bin/www, just after var server = app.listen(.....)

var io = require('').listen(server);

so now I create the ../sockets/base.js file and put this little fellow inside it:

module.exports = function (io) { // io stuff here... io.on('conection..... }

Yeah! Now it works... So i guess i really had no option other than starting inside /bin/www , because that is where my http server was started. The goal is that now i can build socket functionality in other file(s), keeping the thing modular, by require('fileHere')(io);