title: TypeError: Router.use() requires a middleware function but got a undefined
body: <p>Everyone I'm beginner in NODEJS I'm trying to do small chat app.I'm facing routing problem in <strong>server.js.</strong> <strong>[ in the line app.use('/',chatcat.router); ]</strong>.I have attached the error below. Can anyone tell me?.how to resolve this problem. Thanks in advance...</p>
  
  <p>server.js</p>
  
  <pre><code>const express=require('express');
  
  const app=express();
  
  const chatcat=require('./app');
  
  app.set('port',process.env.PORT || 8086);
  
  app.use(express.static('public'));
  
  app.set('view engine','ejs');
  
  app.use('/',chatcat.router);
  
  app.listen(app.get('port'),()=&gt;{
      console.log('server is listening on port: ',app.get('port'));
  });
  </code></pre>
  
  <p>app/index.js</p>
  
  <pre><code>const routes=require('./routes');
  
  module.exports={
       router: routes() 
  }
  </code></pre>
  
  <p>app/routes/index.js:</p>
  
  <pre><code>'use strict';
  
  const h=require('../helpers');
  
  console.log('routers/index outside');
  
  module.exports=()=&gt;{
  
      console.log('routers/index inside');
  
      let routes={
  
          'get':{
              '/':(req,res,next)=&gt;{
                  res.render('login');
              },
              '/chat':(req,res,next)=&gt;{
                  res.render('chatroom');
              },
              '/rooms':(req,res,next)=&gt;{
                  res.render('rooms');
              }
          } ,
          'post':{
              '/chat':(req,res,next)=&gt;{
                res.render('chatroom');
              }
          }    
      }
  
      return h.route12(routes);
  };
  </code></pre>
  
  <p>app/helpers/index.js</p>
  
  <pre><code>'use strict';
  
  const express=require('express');
  
  const router=express.Router();
  
  console.log('helpers/index outside');
  let _registerRoutes=(routes,method)=&gt;{
  
      for(let key in routes){
          if( (typeof routes[key] === 'object') &amp;&amp; (routes[key] !==null) &amp;&amp; !( routes[key] instanceof Array)){
  
              _registerRoutes(routes[key],key);
          }
          else{
              if(method === 'get'){
                  console.log('get in');
                  router.get(key,routes[key]);
              } 
              else if(method ==='post'){
                  console.log('post in');
                  router.post(key,routes[key]);
              }
          }
      }
  }
  
  let route12=routes=&gt;{
      console.log('calling registerroutes');
      _registerRoutes(routes);
  }
  
  module.exports={
      route12
  }
  </code></pre>
  
  <p><strong>I have gotten the error like:</strong></p>
  
  <p>E:\STUDIES\Node Technologies\NodeJS\chatcat\node_modules\express\lib\router\index.js:458
        throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
        ^</p>
  
  <p><strong>TypeError: Router.use() requires a middleware function but got a undefined</strong>
      at Function.use (E:\STUDIES\Node Technologies\All about NodeJS\chatcat\node_modules\express\lib\router\index.js:458:13)
      at Function. (E:\STUDIES\Node Technologies\All about NodeJS\chatcat\node_modules\express\lib\application.js:220:21)
      at Array.forEach ()
      at Function.use (E:\STUDIES\Node Technologies\All about NodeJS\chatcat\node_modules\express\lib\application.js:217:7)
      at Object. (E:\STUDIES\Node Technologies\All about NodeJS\chatcat\server.js:13:5)
      at Module._compile (module.js:653:30)
      at Object.Module._extensions..js (module.js:664:10)
      at Module.load (module.js:566:32)
      at tryModuleLoad (module.js:506:12)
      at Function.Module._load (module.js:498:3)</p>