嗨,我目前正在nodejs上使用hapi框架开发反向代理服务器,有两个简单的任务:
- 将 API 发送到另一台服务器
- 根据任务 1 的响应,将数据与一些静态数据合并
任务 1 没有问题,所有内容都可以通过预先设置正确缓存。但是任务 2 就没那么幸运了,错误如下
Debug: internal, implementation, error server-0 Error: Invalid method key when invoking: replacer server-0 at Object.settings.cache.generateFunc.func [as replacer] (/home/jack/WebstormProjects/dcm-reverse-proxy/node_modules/hapi/lib/methods.js:145:51) server-0 at /home/jack/WebstormProjects/dcm-reverse-proxy/server.js:67:32 server-0 at /home/jack/WebstormProjects/dcm-reverse-proxy/node_modules/hapi/node_modules/catbox/node_modules/hoek/lib/index.js:850:22 server-0 at _combinedTickCallback (internal/process/next_tick.js:67:7) server-0 at process._tickDomainCallback (internal/process/next_tick.js:122:9)
我终于发现服务器方法注册只能接受字符串参数来生成缓存id,
//register
server.method('somemethod', proxyService.replacer, {
cache:{
cache: 'inMemCache',
expiresIn: 5 * 60 * 1000,
generateTimeout: 2000
}
});
//calling, argument1 and argument2 must be {string}
server.methods.somemethod(argument1, argument2, function(_error, _result, cached, _report){
...
});
只是想知道以前是否有人经历过这种情况,如果我想以不同的格式传递 argument1 和 argument2 我该怎么办?
提前致谢。
最佳答案
我自己找到了答案,hapi服务器方法已经记录:
Generate a custom key
In addition to the above options, you may also pass a custom function used to generate a key based on the parameters passed to your method. If your method only accepts some combination of string, number, and boolean values hapi will generate a sane key for you. However, if your method accepts an object parameter, you should specify a function that will generate a key similar to the following. Note: Any arguments that are passed to your method are available to the generateKey method, but not the callback.
关于node.js - 使用服务器方法进行内存缓存中的 Hapi 不接受对象作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37384718/