Cannot set a route in my Node.js Express.js application -
i have node.js application , using express.js framework well. dir structure following:
myapp +-- node_modules +-- public +-- routes |-- index.js |-- mynewpage.js +-- views |-- index.ejs |-- mynewpage.ejs |-- app.js |-- package.json
the app.js
file 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 // 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 29 if ('development' == app.get('env')) { 30 app.use(express.errorhandler()); 31 } 32 33 app.get('/', routes.index); 35 app.get('/mynewpage', routes.mynewpage); /* 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
follows:
exports.mynewpage = function(req, res){ res.render('mynewpage', { title: 'hello' }, function(err, html) {}); };
when try: node app.js
line marked uncommented, error:
/home/myuser/www/app-www/node_modules/express/lib/router/index.js:252 throw new error(msg); ^ error: .get() requires callback functions got [object undefined] @ /home/myuser/www/app-www/node_modules/express/lib/router/index.js:252:11 @ array.foreach (native) @ router.route (/home/myuser/www/app-www/node_modules/express/lib/router/index.js:248:13) @ router.(anonymous function) [as get] (/home/myuser/www/app-www/node_modules/express/lib/router/index.js:270:16) @ function.app.(anonymous function) [as get] (/home/myuser/www/app-www/node_modules/express/lib/application.js:413:26) @ object. (/home/myuser/www/app-www/app.js:35:5) @ module._compile (module.js:456:26) @ object.module._extensions..js (module.js:474:10) @ module.load (module.js:356:32) @ function.module._load (module.js:312:12)
if uncomment line, goes well. what's problem? thankyou
you placed exports.mynewpage
mynewpage.js
file, not referencing anywhere, , trying use routes
object.
while you've called required
./routes
load index.js
, not files folder.
just put code callback routes.js
, work.
Comments
Post a Comment