几个小时以来,我一直困在一个简单的问题上,而且类似的问题也帮不了我。
我之所以使用kraken.js,是因为它具备了我需要的所有特性,而且我的路由也有问题:Error: Route.get() requires callback functions but got a [object Undefined]。
因此,在我的config.json中,我将路由器设置为:“路由器”:{“模块”:{“参数”:{“索引”:“路径:/路由”}}
我的routes.js看起来是这样的:
'use strict';
module.exports = function (router) {
var controllers = require('./controllers');
router.get('/', controllers.index);
//router.get('/offer', controllers.offer);
//router.get('/specifications', controllers.specifications);
router.get('/setLocale/:locale', function (req, res) {
res.cookie('locale', req.params.locale);
res.redirect('/');
});
};我有这样的建筑:
[ROOT]
|-> controllers
|-> index.js
|-> offer.js
|-> specifications.js
|-> routes.js
|-> models
|-> index.js
|-> offer.js
|-> specifications.js
|-> [some other folders and files]这里是我的控制员:
index.js:
'use strict';
var IndexModel = require('../models/index');
exports.index = function(req, res) {
var model = new IndexModel();
res.render('index', model);
};offer.js:
'use strict';
var OfferModel = require('../models/offer');
exports.offer = function(req, res) {
var model = new OfferModel();
res.render('offer', model);
};specifications.js:
'use strict';
var SpecificationsModel = require('../models/specifications');
exports.specifications = function(req, res) {
var model = new SpecificationsModel();
res.render('specifications', model);
};因此,正如您所看到的,3个控制器尊重相同的格式,但只有controller.index回调才能工作。当我取消对controller.[order|specifications]行的注释时,它给了我一个调用未定义对象而不是函数的错误,为什么呢?
另外,我尝试在控制器中的每个导出之前添加module.,然后错误就消失了(这似乎是件好事),但是我告诉我,这些路由不链接到任何文件.
如果有人能帮忙,那就太好了。
发布于 2014-08-11 09:08:02
我找到了一个答案,here。我不能只在一个require语句中要求多个文件。因此,我添加了一个controllers/controllers.js文件,其中包括:
'use strict';
module.exports= {
index : require('./index'),
offer : require('./offer'),
setLocale : require('./setLocale'),
specifications: require('./specifications')
};在我的routes.js中,我可以通过要求./controllers/controllers.js来加载它们
现在我得找出为什么路线有效但页面是空的..。不管怎样,这是另一个问题。泰斯·基利安。
https://stackoverflow.com/questions/25206322
复制相似问题