Hapi新手,正在编写我的第一个应用程序。我将我的路由和控制器放在每个模块中的单独文件中。到目前为止,我已经在我的路由文件中要求控制器如下:
const controller = require('./controller');
然后编写这样的路由:
module.exports = [{
path: '/items/{id}',
method: 'GET',
handler: controller.getItemById
}];
到目前一切尚好。
现在,我想开始在我的控制器方法中使用缓存。这迫使我把我的控制器转换成一个插件。
所以我的控制器现在看起来像:
exports.register = (server, options, next) => {
const itemCache = (id, itemId) => {
// do caching stuff
}
const getItemById = (request, reply) => {
// do stuff, use itemCache
}
server.expose({
getItemById: getItemById
});
next();
};
问题是,现在如何访问路由文件中的控制器?server.dependency()对路由文件不可用。
转载请注明出处:http://www.shandongyidao.com/article/20230526/1026426.html