I am using a the cloud9 ide and getting an error as described in the title. The file structure looks like this
client/
views/
-index.ejs
routes/
-index.js
-server.js
server.js file
var path = require('path');
var async = require('async');
var express = require('express');
var router = express();
var server = http.createServer(router);
router.use(express.static(path.resolve(__dirname, 'client')));
router.set('view engine','ejs');
//app.set('views','app/views');
router.locals.siteTitle = "Meetups";
//router.use(express.static(path.resolve('./public')));
router.use(require('./client/routes/index'));
router.use(require('./client/routes/speaker'));
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
var addr = server.address();
console.log("Chat server listening at", addr.address + ":" + addr.port);
});
While my index.js inside the routes folder which is receiving the error
TypeError: Cannot read property 'get' of undefined router.get('/',function(req,res){
var express = require('express');
var router = express.Router();
router.get('/',function(req,res){
res.render('index',{
pageTitle:'Home',
pageID:'home',
});
});
module.exports = router;
I have copied your code to my c9 ide.
First issue in server.js you need to require http module.
And I have deleted this line because you didn't share this file.
It worked. At least, Node didn't throw any error.
Chat server listening at 0.0.0.0:8080
But I have some recommendations for you.
And import only one file for routes. Import other route files in routes/index.js
I prefer createServer just before server.listen