Hapi.js Catbox Redis返回“server.cache不是一个函数”

所以我99%肯定我只是搞砸了一些愚蠢的东西。

我试图设置catboxcaching对象到redis。 我已经redis和运行,我可以用RDM(SQL专业版像redis)打它,但Hapi不合作。

我这样注册redis catboxcaching:

const server = new Hapi.Server({ cache: [ { name: 'redisCache', engine: require('catbox-redis'), host: 'redis', partition: 'cache', password: 'devpassword' } ] }); 

我这样做在server.js这段代码之后,我继续注册一些更多的插件,并启动服务器。 我也在文件的末尾导出服务器

 module.exports = server; 

然后在我的路线文件中,我试图设置一个testing路线,如下所示:

 { method: 'GET', path: '/cacheSet/{key}/{value}', config: { auth: false }, handler: function(req, res) { const testCache = server.cache({ cache: 'redisCache', expireIn: 1000 }); testCache.set(req.params.key, req.params.value, 1000, function(e) { console.log(e); res(Boom.create(e.http_code, e.message)); }) res(req.params.key + " " + req.params.value); } }, 

注意:我的路由是在一个外部文件中,并被导入到server.js,我注册它们。

如果我注释掉这条路线上的所有caching内容,路由运行正常,并返回我的参数。

如果我运行这个caching的东西,起初我有“服务器没有定义”。 所以我补充说

 const server = require('./../server.js'); 

导入服务器。

现在,当我运行这个,我得到“server.cache不是一个函数”和一个500错误。

我不明白我做错了什么。 我的猜测是我正在导入服务器,但也许是没有设置所有configuration的对象,所以无法使用.cache方法。 然而,这似乎是错误的,因为.cache应该总是一个默认的方法与默认的内存caching,所以即使我的caching注册尚未活动,server.cache理论上仍然是一种方法。

我知道它必须是一个基本的东西搞乱了,但是什么?

我是对的。 我正在做一些愚蠢的事情。 这与我如何导出我的服务器有关。 我修改了我的结构来取消最初的服务器创build,并使其更加模块化。 现在我只是简单地导出服务器像这样:

 'use strict'; const Hapi = require('hapi'); const server = new Hapi.Server({ cache: [ { name: 'redisCache', engine: require('catbox-redis'), host: 'redis', partition: 'cache', password: 'devpassword' } ] }); module.exports = server; 

然后,我将其导入到我的主要服务器文件(现在index.js以前server.js),一切运行良好。 我也可以导入到任何其他文件(在这种情况下我的路线文件),并访问服务器的适当方法。

Redis很高兴地存储密钥,Hapi很高兴地不给我错误。

如果任何人遇到这样的愚蠢错误,就离开这里。