I'm starting on node JS and I'm facing a problem with IO lib, here error:
Reference error: IO is not defined in export. Index (D: \ dev \ lib \ index.js: 9: 5) on callback (D: \ dev \ node_modules \ express \ lib \ router \ index.js: 164: 37) Ultimate (D: \ dev \ node_modules \ Express \ lib \ router \ index.js: 138: 11) pass (D: \ dev \ node_modules \ express \ lib \ router \ index.js: 145: 5) on the router. _ Dispatch (D: \ God \ MIDIFx \ XP \ Lib \ Router \ Index. JS: 1 73: 5) Object. On the Object.methodOverride [handle: reverse (D: \ dev \ anode_modules \ express \ lib / router \ index.js (D: \ dev \ node_modules \ express \ node_modules \ connect \ lib \ proto.js: 190: 15) (In the form of [handle] node_modules \ connect \ lib \ middleware \ methodOverride.js: 49: 5) (D: \ dev \ node_modules \ express \ Object.urlencoded at the next (D: \ dev \ node_modules \ express \ Node_modules \ connect \ lib \ proto.js: 190: 15) (D: \ dev \ node_modules \ express \ node_modules \ connect \ lib \ Middleware \ urlencoded.js: 51: 37)
< P> Here is the content of my app .js file: var express = require ('express'); var path = requirement ('path'); var app = a ('Socket.io'). Need (server), fs = need ('fs'); app .set ('port', process.env.PORT || 8080); App.set ('idea', path .join (__deirnam, 'idea')); app.set ('visual engine', 'jade' ); App.use (express.favicon ()); App.use (express.logger ('dev')); App.use (express.json ()); App.use (express.urlencoded ()); App.use (express.methodOverride ()); App.use (app.router); App Use (express.static (path.join (__derename, 'public')); App.get ('/', Required ('. / Lib'). Index); Server.listen (app.get ('port'), function () {console.log ('listen to express server on port' + app.get ('port'));};
My files / lib / index.js:
exports.index = function (req, res) {res.render ('index'); Io.sockets.on ('connection', function (socket) {socket.emit ('message', 'welcome');}); };
Can anyone help me?
You have to pass io
on your way:
exports.index = function (io) {return function (rick, ridge) {res.render ('index'); Io.sockets.on ('connection', function (socket) {socket.emit ('message', 'welcome')}}}}
and then as a function Call app.get
:
requires app.get ('/', ('./bin'). Index (io));
I recommend the declaration of part ('. /' '>>) with the other variables of your code. This code reuse and improved readability ( Which is relative to each person.
var / * other variables * /, lib = is required ('. / Lib');
then you can just do the app
Just get an idea. (/ ', Lib.index (io)).
< / Div>