hapi:16.6.2,h2o2:5.2.0
我有h2o2 proxy路由使用外部API
{
method: 'GET',
path: '/api/v3/{param*}',
handler: {
proxy: {
host: 'host.net',
port: 8100,
protocol: 'http',
}
}
}
此API使用客户端会话。要维护会话,cookie值应位于请求标头内,例如:
Cookie: SESSION=352abdc5-c0de-49e3-985e-e07c7ab26a88
如何在h2o2中添加cookie值?
更新。
有onRequest可以访问上游请求。但是Node.js会抛出错误
ValidationError: Invalid proxy handler options (/api/v3/{param*}) {
"host": "host.net",
"port": 8100,
"protocol": "http",
"onRequest" [1]: function onRequest(req) {\n console.log(req);\n }
}
[1] "onRequest" is not allowed
答案 0 :(得分:0)
mapUri方法可用于拦截上游请求。
以下是h2o2代理路由的示例,其中将cookie添加到请求标头中:
const host = 'host.net';
const port = 8100;
const protocol = 'http';
const path = '/api/v3/{param*}';
const authCookie = 'SESSION=352abdc5-c0de-49e3-985e-e07c7ab26a88';
export default [
{
method: 'GET',
path,
handler: {
proxy: {
mapUri: function(req, cb) {
let {path, headers} = req;
const url = `${protocol}://${host}:${port}${path}`;
headers.cookie = authCookie;
return cb(null, url, headers);
},
onResponse: function(err, res, req, reply) {
return reply(res);
},
},
},
},
];