0

I have my Node.js application and I am using the Express.js framework as well. The dir structure is the following:

myapp
  +-- node_modules
  +-- public
  +-- routes
         |-- index.js
         |-- mynewpage.js
  +-- views
         |-- index.ejs
         |-- mynewpage.ejs
  |-- app.js
  |-- package.json

The app.js file is like this:

 2  /**
 3   * Module dependencies.
 4   */
 5  
 6  var express = require('express')
 7    , routes = require('./routes')
 9    , http = require('http')
10    , path = require('path');
11  
12  var app = express();
13  
14  // all environments
15  app.set('port', process.env.PORT || 3000);
16  app.set('views', __dirname + '/views');
17  app.set('view engine', 'ejs');
18  app.use(express.favicon());
19  app.use(express.logger('dev'));
20  app.use(express.bodyParser());
21  app.use(express.methodOverride());
22  app.use(express.cookieParser('your secret here'));
23  app.use(express.session());
24  app.use(app.router);
25  app.use(require('less-middleware')({ src: __dirname + '/public' }));
26  app.use(express.static(path.join(__dirname, 'public')));
27  
28  // development only
29  if ('development' == app.get('env')) {
30    app.use(express.errorHandler());
31  }
32  
33  app.get('/', routes.index);
35  app.get('/mynewpage', routes.mynewpage); /* THIS LINE */
36  
37  http.createServer(app).listen(app.get('port'), function(){
38    console.log('Express server listening on port ' + app.get('port'));
39  });

File mynewpage.js is as follows:

exports.mynewpage = function(req, res){
  res.render('mynewpage', { title: 'Hello' }, function(err, html) {});
};

When I try: node app.js with the line I marked uncommented, I get this error:

/home/myuser/www/app-www/node_modules/express/lib/router/index.js:252 throw new Error(msg); ^ Error: .get() requires callback functions but got a [object Undefined] at /home/myuser/www/app-www/node_modules/express/lib/router/index.js:252:11 at Array.forEach (native) at Router.route (/home/myuser/www/app-www/node_modules/express/lib/router/index.js:248:13) at Router.(anonymous function) [as get] (/home/myuser/www/app-www/node_modules/express/lib/router/index.js:270:16) at Function.app.(anonymous function) [as get] (/home/myuser/www/app-www/node_modules/express/lib/application.js:413:26) at Object. (/home/myuser/www/app-www/app.js:35:5) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12)

If I uncomment that line, everything goes well. What's the problem? Thankyou

Andry
  • 16,172
  • 27
  • 138
  • 246

2 Answers2

3

You placed exports.mynewpage into mynewpage.js file, but not referencing it anywhere, and trying to use it from routes object.
While you've called required for ./routes it will load only index.js, but not all files from that folder.

Just put the code for that callback into routes.js and it will work.

moka
  • 22,846
  • 4
  • 51
  • 67
  • it is referenced in app.js: the first lines, you can see require('./routes') directive... – Andry Jul 11 '13 at 10:20
  • 1
    But it will not include all .js files, only index.js. If you want to include all of them, then you need to reference each file individually. Check this answer http://stackoverflow.com/questions/5364928/node-js-require-all-files-in-a-folder – moka Jul 11 '13 at 10:27
  • Thank you a lot and sorry for doubting :) – Andry Jul 11 '13 at 15:13
  • 1
    'You can't have faith without doubt' - Abraham – moka Jul 11 '13 at 15:44
0
var mynewpage = require('./routes/mynewpage');

will have to be required in order to call this the original way you created.

venergiac
  • 7,469
  • 2
  • 48
  • 70