Hapi.js Catbox Redis 返回 "server.cache is not a function"
Hapi.js Catbox Redis returning "server.cache is not a function"
所以我 99% 确定我只是搞砸了一些蠢事。
我正在尝试设置 catbox 以将对象缓存到 redis。我已经启动了 Redis 并 运行ning 并且我可以使用 RDM(sql pro like redis 实用程序)但是 Hapi 不合作。
我这样注册redis catbox缓存:
const server = new Hapi.Server({
cache: [
{
name: 'redisCache',
engine: require('catbox-redis'),
host: 'redis',
partition: 'cache',
password: 'devpassword'
}
]
});
我在 server.js 中执行此操作 在这段代码之后,我继续注册更多插件并启动服务器。我也在文件末尾导出服务器
module.exports = server;
然后在我的路由文件中,我尝试设置一条测试路由,如下所示:
{
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中。
如果我注释掉这条路线上的所有缓存内容,路线 运行 没问题并且 returns 我的参数。
如果我 运行 使用缓存内容,首先我得到 "server not defined"。所以我然后添加
const server = require('./../server.js');
导入服务器。
现在当我 运行 这个时,我得到 "server.cache is not a function" 和一个 500 错误。
我不明白我做错了什么。我的猜测是我正在导入服务器,但也许它是没有设置所有配置的对象,因此它无法使用 .cache 方法。然而,这似乎是错误的,因为 .cache 应该始终是默认内存缓存的默认方法,所以即使我的缓存注册尚未激活,server.cache 理论上应该仍然是一种方法。
我知道我搞砸了一定是一些基本的东西,但是什么?
我是对的。我在做一些愚蠢的事情。这与我导出服务器的方式有关。我修改了我的结构以提取初始服务器创建并使其更加模块化。现在我只是像这样导出服务器:
'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 很高兴没有给我错误。
离开这里以防其他人遇到这样的愚蠢错误。
所以我 99% 确定我只是搞砸了一些蠢事。
我正在尝试设置 catbox 以将对象缓存到 redis。我已经启动了 Redis 并 运行ning 并且我可以使用 RDM(sql pro like redis 实用程序)但是 Hapi 不合作。
我这样注册redis catbox缓存:
const server = new Hapi.Server({
cache: [
{
name: 'redisCache',
engine: require('catbox-redis'),
host: 'redis',
partition: 'cache',
password: 'devpassword'
}
]
});
我在 server.js 中执行此操作 在这段代码之后,我继续注册更多插件并启动服务器。我也在文件末尾导出服务器
module.exports = server;
然后在我的路由文件中,我尝试设置一条测试路由,如下所示:
{
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中。
如果我注释掉这条路线上的所有缓存内容,路线 运行 没问题并且 returns 我的参数。
如果我 运行 使用缓存内容,首先我得到 "server not defined"。所以我然后添加
const server = require('./../server.js');
导入服务器。
现在当我 运行 这个时,我得到 "server.cache is not a function" 和一个 500 错误。
我不明白我做错了什么。我的猜测是我正在导入服务器,但也许它是没有设置所有配置的对象,因此它无法使用 .cache 方法。然而,这似乎是错误的,因为 .cache 应该始终是默认内存缓存的默认方法,所以即使我的缓存注册尚未激活,server.cache 理论上应该仍然是一种方法。
我知道我搞砸了一定是一些基本的东西,但是什么?
我是对的。我在做一些愚蠢的事情。这与我导出服务器的方式有关。我修改了我的结构以提取初始服务器创建并使其更加模块化。现在我只是像这样导出服务器:
'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 很高兴没有给我错误。
离开这里以防其他人遇到这样的愚蠢错误。