首先,我想说我是Hapi的新手,请不要对我太苛刻。
我正在按照这个教程尝试使用catbox
和catbox-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(根据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();
你有没有想过我做错了什么?我在这个问题上花费了很多时间,所以非常感谢任何帮助。