hapijs v17 在插件中发送响应之前设置 header
hapijs v17 set header before sending response in plugin
我想为 v17 创建一个 HapiJS 插件以附加到响应中一些自定义 headers,对于 cors 但应该无关紧要。
const cors = {
name: 'cors',
version: '1.0.0',
async register(server) {
logger.debug('Initiating CORS internal plugin...');
const { port } = server;
originPort = port;
server.ext('onPreResponse', async (request, h) => {
return h
.response()
.header('Access-Control-Allow-Origin', resOrigin)
.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE')
.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Authorization')
.header('Access-Control-Allow-Credentials', 'true')
.header('Access-Control-Max-Age', 1728000);
});
},
};
这是我目前所拥有的。我不认为这是正确的,因为我得到一个错误。
我也试过:
server.ext('onPreResponse', async (request, h) => {
const { response } = request;
response.header('Access-Control-Allow-Origin', resOrigin);
response.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE');
response.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Authorization');
response.header('Access-Control-Allow-Credentials', 'true');
response.header('Access-Control-Max-Age', 1728000);
return h.continue;
});
这似乎在本地有效,但在测试期间失败了。
console.error node_modules/hapi/lib/core.js:126
Debug: internal, implementation, error
TypeError: response.header is not a function
at header (/home/node/app/src/lib/cors.js:47:22)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
如何在 HapiJS v17 中实现这一点? API 似乎已在此版本中完全重构,并且很少有示例说明我在此版本的 Hapi 中具体尝试做什么。
谢谢!
当您 return Boom 错误时,您的响应行为会发生变化。试试这个。
exports.plugin = {
async register(server, options) {
server.ext('onPreResponse', async (request, h) => {
const {response} = request;
if (response.isBoom) {
response.output.headers['Access-Control-Allow-Credentials'] = 'true';
response.output.headers['Access-Control-Allow-Methods'] = 'GET,POST,PUT,DELETE';
} else {
response.header('Access-Control-Allow-Credentials', 'true');
response.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE');
}
return h.continue;
});
},
name: 'cors'
};
我想为 v17 创建一个 HapiJS 插件以附加到响应中一些自定义 headers,对于 cors 但应该无关紧要。
const cors = {
name: 'cors',
version: '1.0.0',
async register(server) {
logger.debug('Initiating CORS internal plugin...');
const { port } = server;
originPort = port;
server.ext('onPreResponse', async (request, h) => {
return h
.response()
.header('Access-Control-Allow-Origin', resOrigin)
.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE')
.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Authorization')
.header('Access-Control-Allow-Credentials', 'true')
.header('Access-Control-Max-Age', 1728000);
});
},
};
这是我目前所拥有的。我不认为这是正确的,因为我得到一个错误。
我也试过:
server.ext('onPreResponse', async (request, h) => {
const { response } = request;
response.header('Access-Control-Allow-Origin', resOrigin);
response.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE');
response.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Authorization');
response.header('Access-Control-Allow-Credentials', 'true');
response.header('Access-Control-Max-Age', 1728000);
return h.continue;
});
这似乎在本地有效,但在测试期间失败了。
console.error node_modules/hapi/lib/core.js:126
Debug: internal, implementation, error
TypeError: response.header is not a function
at header (/home/node/app/src/lib/cors.js:47:22)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
如何在 HapiJS v17 中实现这一点? API 似乎已在此版本中完全重构,并且很少有示例说明我在此版本的 Hapi 中具体尝试做什么。
谢谢!
当您 return Boom 错误时,您的响应行为会发生变化。试试这个。
exports.plugin = {
async register(server, options) {
server.ext('onPreResponse', async (request, h) => {
const {response} = request;
if (response.isBoom) {
response.output.headers['Access-Control-Allow-Credentials'] = 'true';
response.output.headers['Access-Control-Allow-Methods'] = 'GET,POST,PUT,DELETE';
} else {
response.header('Access-Control-Allow-Credentials', 'true');
response.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE');
}
return h.continue;
});
},
name: 'cors'
};