I'm beginning in NodeJS, but my code have a problem. I'll show the code.
I need load the application in MVC pattern, I'm using the express-load, but it gives error when I create the load order.
app.js (The important part)
var load = require('express-load');
...
load('models').then('controllers').then('routes').into(app);
controllers/home.js
module.exports = function(app){
var HomeController = {
index: function(req,res) {
res.render('home/index');
}
}
return HomeController;
}
routes/home.js
module.exports = function(app){
var home = app.controllers.home;
app.get('/home',home.index);
}
Error
npm start
> 4cash@0.0.1 start /Users/ewertonmelo/Documents/projects/4cash
> node ./bin/www
/Users/ewertonmelo/Documents/projects/4cash/node_modules/express/lib/router/index.js:120
var search = 1 + req.url.indexOf('?');
^
TypeError: Cannot call method 'indexOf' of undefined
at Function.proto.handle(/Users/ewertonmelo/Documents/projects/4cash/node_modules/express/lib/router/index.js:120:28)
at Object.router(/Users/ewertonmelo/Documents/projects/4cash/node_modules/express/lib/router/index.js:27:12)
at /Users/ewertonmelo/Documents/projects/4cash/node_modules/express-load/lib/express-load.js:232:17
at iterate (/Users/ewertonmelo/Documents/projects/4cash/node_modules/express-load/node_modules/async/lib/async.js:131:13)
at /Users/ewertonmelo/Documents/projects/4cash/node_modules/express-load/node_modules/async/lib/async.js:142:25
at /Users/ewertonmelo/Documents/projects/4cash/node_modules/express-load/lib/express-load.js:252:7
at iterate (/Users/ewertonmelo/Documents/projects/4cash/node_modules/express-load/node_modules/async/lib/async.js:131:13)
at /Users/ewertonmelo/Documents/projects/4cash/node_modules/express-load/node_modules/async/lib/async.js:142:25
at /Users/ewertonmelo/Documents/projects/4cash/node_modules/express-load/lib/express-load.js:252:7
at iterate (/Users/ewertonmelo/Documents/projects/4cash/node_modules/express-load(/node_modules/async/lib/async.js:131:13)
npm ERR! 4cash@0.0.1 start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the 4cash@0.0.1 start script.
npm ERR! This is most likely a problem with the 4cash package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node ./bin/www
npm ERR! You can get their info via:
npm ERR! npm owner ls 4cash
npm ERR! There is likely additional logging output above.
npm ERR! System Darwin 13.3.0
npm ERR! command "node" "/usr/local/bin/npm" "start"
npm ERR! cwd /Users/ewertonmelo/Documents/projects/4cash
npm ERR! node -v v0.10.30
npm ERR! npm -v 1.4.21
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/ewertonmelo/Documents/projects/4cash/npm-debug.log
npm ERR! not ok code 0
Because not even use models, modified the load
load('controllers').then('routes').into(app);
The controller and routes exists, but give error. I want load my app in MVC pattern.
Remove all the files under routes folder except home.js. I think you have the default index and users routing files which create these problems.
You can remove the following lines form index.js and users.js file to avoid problems.
var express = require('express');
var router = express.Router();
Sorry for my English.