我的努力是将我的应用程序从Express重新设计为Koa,因此routes.js和controllers.js之前工作。我尝试创建一个小机制来渲染Koa中的模板。目标是通过每个module.routes调用module.view_engine的渲染变量,自动为其他模块加载不同模板引擎的工作代码。你能帮忙吗?
+---modules
| +---core
| | +---client
| | | \---views
| | | equipment.pug
| | | error.pug
| | | home.pug
| | |
| | \---server
| | +---configs
| | | core.config.json
| | | core.koa.configs.js
| | |
| | +---controllers
| | | core.server.controllers.js
| | |
| | +---models
| | | assembly.server.model.js
| | | ecms_equipment.server.model.js
| | | ecms_location.server.model.js
| | | ecms_main.server.model.js
| | | index.js
| | |
| | \---routes
| | core.server.routes.js
| |
| +---assemblys
| | +---client
| | | \---views
| | | assembly_view.pug
| | |
| | \---server
| | +---controllers
| | | assemblys.server.controllers.js
| | |
| | \---routes
| | assemblys.server.routes.js
那是我的目录结构。每个循环都有几个Lodash,我能够在core.koa.configs.js中为每个模块实现view_path,routes,view_engine
modules[module] = {
view_path: view_path,
routes: routes,
view_engine: view_engine
};
在同一个文件中,module
循环中仍有_.each
,我会调用
// regex to narrow the scope of code experimenting
if (module.re('assemblys')) {
// === var render here? ===
require(routes)(app);
}
将app带到包含以下内容的assembly.server.routes.js:
var controller = require('../controllers/projects.server.controllers');
module.exports = function(app){
app.get('/task', controller.assembly)
};
然后,在assembly.server.controllers.js中,我尝试提供汇编函数,但它不起作用 - 404:
// load Sequelize models
var models = require( process.cwd() + '/modules/core/server/models');
exports.assembly = function *() {
var that = this.body, views = require('co-views');
var render = views('../../client/views/' , {
map: {pug: 'jade'}
});
var Assembly = models.Assembly;
Assembly.findAll().then(function (assemblys) {
that = yield render('assembly_view' , {assemblys: assemblys});
});
};
以前,我将var render
放在core.configs中以便能够在那里使用view_path但是当我将render
传递到function *()
时,我收到错误并且应用程序崩溃了!当render在core.configs中时,我有routes.js文件导出这个:
module.exports = function(app, render){
app.get('/task', controller.assembly(render));
};
一些Koa示例说明在此route
中使用app.use(route.get('/assembly', function *() { });
我看到其他一些例子只是使用这个
app.get('/task', controller.assembly(render)); // cnpmjs.org, koan stack...
我更喜欢后者。但是如果遇到问题yield render
以及如何加载应用,将参数渲染到function *(app,render)
生成器中,我尝试了这个但是它也没有用。堆栈跟踪错误。
想法结果是核心.koa.configs.js :
if (module.re('assemblys')) {
var render = views(view_path , {
map: view_engine
});
require(routes)(app, render);
}
然后导出路线
module.exports = function(app, render){
app.get('/task', controller.assembly(render))
};
并且控制器能够渲染组件:
exports.assembly = function *(render) {
var that = this.body, views = require('co-views');
var Assembly = models.Assembly;
Assembly.findAll().then(function (assemblys) {
that = yield render('assembly_view' , {assemblys: assemblys});
});
};
更新:请参阅stack-traces,请参阅koa branch_commit。 -compiled.js.map文件由ES6-Babel
生成答案 0 :(得分:0)
Koa(与express不同)不会等到你设置响应体。如果您的中间件功能结束,并且响应正文为空,则会给出404响应。这就是为什么,在koa你应该避免回调或宣传它们。
以下是koa风格的中间件功能:
exports.assembly = function *(render) {
var that = this.body, views = require('co-views');
var Assembly = models.Assembly;
var assemblys = yield Assembly.findAll();
that = yield render('assembly_view' , {assemblys: assemblys});
};