尝试使用快速加载和expression4

我开始在NodeJS,但我的代码有一个问题。 我将显示代码。

我需要以MVC模式加载应用程序,我正在使用express-load ,但是当我创build加载顺序时会出错。

app.js(重要部分)

 var load = require('express-load'); ... load('models').then('controllers').then('routes').into(app); 

控制器/ home.js

 module.exports = function(app){ var HomeController = { index: function(req,res) { res.render('home/index'); } } return HomeController; } 

路线/ home.js

 module.exports = function(app){ var home = app.controllers.home; app.get('/home',home.index); } 

错误

 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 

因为甚至没有使用模型,修改了load

 load('controllers').then('routes').into(app); 

控制器和路由存在,但给出错误。 我想以MVC模式加载我的应用程序。

删除home.js以外的路由文件夹下的所有文件。 我认为你有默认的索引和用户路由文件,造成这些问题。

您可以从index.jsusers.js文件中删除以下行,以避免出现问题。

 var express = require('express'); var router = express.Router(); 

对不起我的英语不好。