NodeJS 使用@hapi 21.3.2实现缓存系统时出错

7rtdyuoh  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(155)

我被要求将我们的hapi服务器从版本11升级到最新版本(21.3.2),这需要相当多的重构。我已经设法让服务器运行,一切似乎都正常工作,但我得到一个错误,当我试图添加一个缓存系统,我不能弄清楚,如果问题是与我的实现或没有。
我的环境:

Node : 16.20.2
@hapi/hapi : 21.3.2
@hapi/catbox : 12.1.1
@hapi/catbox-memory : 6.0.1

下面是我启动服务器时得到的错误

TypeError: refAnnotations.errors[cacheKey].push is not a function
    at exports.ValidationError.exports.error [as annotate] (...path/server/node_modules/@hapi/validate/lib/annotate.js:53:53)

这就是我的服务器是如何初始化的,以及该高速缓存系统是如何实现的:

const Hapi = require('@hapi/hapi');
const catboxMemory = require('@hapi/catbox-memory');
... rest of imports

const init = async () => {

    const server = Hapi.Server({
        host: 'localhost',
        port: config.port,
        cache:
        {
            name: 'memoryCache',
            provider: {
                constructor: catboxMemory
            }
        },
    });

    // Register the plugins
    await server.register([
        H2o2,
        Inert,
        Vision,
        dataPlugin,
        routesPlugin
    ]);

    const start = async () => {
         
                ... function that handle views

        await server.start();
    };

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

process.on('unhandledRejection', (err) => {
    console.log(err);
    process.exit(1);
});

init();

我已经尝试了几种不同的语法,但是hapi文档对我没有多大帮助,我看不出我错过了什么。

cbjzeqam

cbjzeqam1#

我偶然在hapi的更新日志中找到了我问题的答案。我不得不调用catBoxMemory的引擎。下面是一个例子:

const server = Hapi.Server({
        host: 'localhost',
        port: config.port,
        cache:
        {
            name: 'memoryCache',
            provider: {
                constructor: catboxMemory.Engine
            }
        },
    });

相关问题