Everyone I'm beginner in NODEJS I'm trying to do small chat app.I'm facing routing problem in server.js. [ in the line app.use('/',chatcat.router); ].I have attached the error below. Can anyone tell me?.how to resolve this problem. Thanks in advance...
server.js
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'),()=>{
console.log('server is listening on port: ',app.get('port'));
});
app/index.js
const routes=require('./routes');
module.exports={
router: routes()
}
app/routes/index.js:
'use strict';
const h=require('../helpers');
console.log('routers/index outside');
module.exports=()=>{
console.log('routers/index inside');
let routes={
'get':{
'/':(req,res,next)=>{
res.render('login');
},
'/chat':(req,res,next)=>{
res.render('chatroom');
},
'/rooms':(req,res,next)=>{
res.render('rooms');
}
} ,
'post':{
'/chat':(req,res,next)=>{
res.render('chatroom');
}
}
}
return h.route12(routes);
};
app/helpers/index.js
'use strict';
const express=require('express');
const router=express.Router();
console.log('helpers/index outside');
let _registerRoutes=(routes,method)=>{
for(let key in routes){
if( (typeof routes[key] === 'object') && (routes[key] !==null) && !( 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=>{
console.log('calling registerroutes');
_registerRoutes(routes);
}
module.exports={
route12
}
I have gotten the error like:
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)) ^
TypeError: Router.use() requires a middleware function but got a undefined 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)