Hapi Catbox无法连接到Redis服务器。

3

首先,我想说我是Hapi的新手,请不要对我太苛刻。

我正在按照这个教程尝试使用catboxcatbox-redisnpm包来设置基于Redis的服务器端缓存,但我遇到了以下错误:

{
  reason: Error: Connection is closed.
      at Redis.connectionCloseHandler (/home/yuriy/dev/sources/all/hapi-getting-started/node_modules/ioredis/built/redis/index.js:305:24)
      at Object.onceWrapper (events.js:300:26)
      at Redis.emit (events.js:210:5)
      at processTicksAndRejections (internal/process/task_queues.js:75:11)
}

如您所见,它说错误出现在ioredis(根据package-lock.json中的版本为v4.14.1)包中,该包是catbox-redis的依赖项。

我在本地运行Redis服务器。

username@my-computer:~$ redis-cli -v
redis-cli 4.0.9
username@my-computer:~$ redis-cli 
127.0.0.1:6379> ping
PONG

这是我的package.json文件:

{
  "name": "hapi-getting-started",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.ts",
  "scripts": {
    "build": "rimraf dist && tsc",
    "start": "rimraf dist && tsc && node dist/index.js",
    "dev": "tsc -w | nodemon dist/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@hapi/catbox": "^10.2.3",
    "@hapi/catbox-redis": "^5.0.5",
    "@hapi/hapi": "^18.4.0",
    "rimraf": "^3.0.0",
    "typescript": "^3.7.2"
  },
  "devDependencies": {
    "@types/hapi__catbox": "^10.2.2",
    "@types/hapi__catbox-redis": "^5.0.0",
    "@types/hapi__hapi": "^18.2.6",
    "@types/node": "^12.12.14",
    "nodemon": "^2.0.1"
  }
}

这是我的 src/index.ts 文件:

const Hapi = require('@hapi/hapi');
const CatboxRedis = require('@hapi/catbox-redis');

console.log(`Running environment ${process.env.NODE_ENV || 'dev'}`);

// Catch uncaught exceptions
process.on('uncaughtException', (error: Error) => {
  console.error(`uncaughtException ${error.message}`);
  console.error({ reason });
});

// Catch unhandled rejected promises
process.on('unhandledRejection', (reason: any) => {
  console.error(`unhandledRejection ${reason}`);
  console.error({ error });
});

const init = async () => {
  const server = Hapi.server({
    host: 'localhost',
    port: 8000,
    cache: {
      name: 'redis-cache',
      provider: {
        constructor: CatboxRedis,
        options: {
          partition: 'my_cached_data',
          tls: {},
        },
      },
    },
  });

  await server.start();
  console.log('Server running on %s', server.info.uri);
};

init();

你有没有想过我做错了什么?我在这个问题上花费了很多时间,所以非常感谢任何帮助。

1个回答

8

好的,看起来在Hapi官方文档的缓存部分中存在问题。解决方法非常简单但不明显:我只是删除了tls:{},

  const server = Hapi.server({
    host: 'localhost',
    port: 8000,
    cache: {
      name: 'redis-cache',
      provider: {
        constructor: CatboxRedis,
        options: {
          partition: 'my_cached_data',
          // tls: {}, <-- Here is a problem, remove this line
        },
      },
    },
  });

这是关于 ioredis 配置参数的内容,来源于 catbox-redis文档

tls - 表示 ioredis 的 TLS 配置选项的对象。

你可以在 ioredis文档中找到更多细节。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接