I am developing a simple chat application using node.js and socket.io. When I am connecting to the socket.io then I face an error of "cannot read property 'method' of undefined".
Here is the app.js file code:-
var express = require('express'),
app = express(),
path = require('path')
cookieParser = require('cookie-parser')
session = require('express-session')
config = require('./config/config.js'),
ConnectMongo = require('connect-mongo')(session),
mongoose = require('mongoose').connect(config.dbURL),
passport = require('passport'),
FacebookStrategy = require('passport-facebook').Strategy,
rooms = [] ;
app.set('views', path.join(__dirname , 'views'));
app.engine('html', require('hogan-express'));
app.set('view engine', 'html');
app.use(express.static(path.join(__dirname,'public')));
app.use(cookieParser());
var env = process.env.NODE_ENV || 'development';
if(env === 'development'){
// dev specific settings
app.use(session({secret:config.sessionSecret, saveUninitialized : true, resave : true}));
} else {
//Production specific settings
app.use(session({
secret : config.sessionSecret,
saveUninitialized : true,
resave : true,
store : new ConnectMongo({
// url : config.dbURL, (for Avoiding two seperate connection)
mongooseConnection:mongoose.connections[0],
stringify : true
})
}));
}
app.use(passport.initialize());
app.use(passport.session());
require('./auth/passportAuth.js')(passport , FacebookStrategy , config , mongoose);
require('./routes/routes.js')(express,app ,passport , config);
/* app.listen (3000 , function(){
console.log("ChatUp working on the Port 3000");
console.log('Mode:'+ env);
}); */
app.set('port', process.env.PORT || 3000);
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
require('./socket/socket.js')(io,rooms);
server.listen(app.get('port' , function(){
console.log('Chat Up on Port : '+ app.get('port'));
}))
And Here is my Chatrooms.html srcipt :-
$(function() {
var host = '{{config.host}}';
var socket = io.connect(host + '/roomlist'); // http://localhost:3000/roomlist
socket.on('connect', function() {
console.log('Connection Established .. !');
})
})
JSON file contain "host":"http://localhost:3000"
But when I run the code there is an error :TypeError : cannot read property 'method' of undefined
Console Error :-
c:\Users\ANKUR SINGH\Desktop\node\Chat App\node_modules\express\lib\router\index.js:138 debug('dispatching %s %s', req.method, req.url); ^
TypeError: Cannot read property 'method' of undefined
at Function.handle (c:\Users\ANKUR SINGH\Desktop\node\Chat App\node_modules\express\lib\router\index.js:138:33)
at EventEmitter.handle (c:\Users\ANKUR SINGH\Desktop\node\Chat App\node_modules\express\lib\application.js:173:10)
at Server.app (c:\Users\ANKUR SINGH\Desktop\node\Chat App\node_modules\express\lib\express.js:38:9)
at Server.g (events.js:260:16)
at emitNone (events.js:67:13)
at Server.emit (events.js:166:7)
at emitListeningNT (net.js:1263:10)
at nextTickCallbackWith1Arg (node.js:444:9)
at process._tickCallback (node.js:366:17)
at Module.runMain [as _onTimeout] (module.js:432:11)
at Timer.listOnTimeout (timers.js:92:15)
How to get rid of this error. ??
Picture of : /router/index.js file :-