Hapi catbox无法连接到redis服务器

Hapi catbox not able to connect to redis server

首先,我想说我是 Hapi 的新手,所以请不要苛刻地评判我。

我正在关注 this tutorial 尝试使用 catboxcatbox-redis npm 包设置基于 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(v4.14.1,根据 package-lock.json)包,它是 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();

知道我做错了什么吗?我在这个问题上花了很多时间,所以非常感谢任何帮助。

好的,Hapi official docs on caching(服务器端缓存部分)似乎有问题。解决方案非常简单但并不明显:我只是删除了 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 docs:

tls - an object representing TLS config options for ioredis.

您可以在 ioredis docs 中找到更多详细信息。