最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError : cannot read property 'method' of undefined in nodejs Application - Stack Overflow

programmeradmin3浏览0评论

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

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 :-

Error picture

Share Improve this question edited Jan 16, 2016 at 10:01 Ankur Singh asked Jan 16, 2016 at 8:47 Ankur SinghAnkur Singh 9362 gold badges9 silver badges19 bronze badges 13
  • where you see this error? – Grundy Commented Jan 16, 2016 at 8:49
  • When I run the app using Window CMD :- node app.js – Ankur Singh Commented Jan 16, 2016 at 8:51
  • It appear in the console : TypeError : cannot read property 'method' of undefined – Ankur Singh Commented Jan 16, 2016 at 8:53
  • so, do you see any additional info? like line where is error? – Grundy Commented Jan 16, 2016 at 8:55
  • plzz Help me #Grundy – Ankur Singh Commented Jan 16, 2016 at 9:11
 |  Show 8 more ments

4 Answers 4

Reset to default 8

After 9 hours of continuous struggling. I have the found the solution of my question.

The error code :-

app.set('port', process.env.PORT || 3000 );

server.listen(app.get('port' , function(){
     console.log('Chat Up on Port : '+ app.get('port'));
 }));    

And the Correct code :-

var port = process.env.PORT || 3000;
  
  server.listen(port, function () {
    console.log('Updated : Server listening at port %d', port);
  }); 

The reference link is:- Correct Code reference

Your code would work. There is a missing ')'

app.set('port', process.env.PORT || 3000 );
server.listen(app.get('port') , function(){
console.log('Chat Up on Port : '+ app.get('port'));
}); 

if you are using app.js as express and start.js as the "npm start", all you have to do is to make sure the application was exported module.exports = app; and on the start.js you just have to require the module require('./app') this works fine.

Aren't you missing some mas when declaring your variables?

path = require('path')
cookieParser = require('cookie-parser')
session = require('express-session')

path = require('path'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
发布评论

评论列表(0)

  1. 暂无评论