我正在尝试利用fastify和fastify-http-proxy来代理一些请求的遗留web服务器。
fastify-http-proxy代码库中的示例代码:
const Fastify = require('fastify')
const server = Fastify()
server.register(require('fastify-http-proxy'), {
upstream: 'http://my-legacy-webserver.com',
prefix: '/legacy'
})
server.listen(3000)它按预期工作,但一些代理请求可能会返回404,遗留but服务器呈现其自定义的404页面,该页面被代理到客户端。我希望拦截404 (可能是每40x,也可能是50x)响应,并在我的fastify服务器中处理它们。有可能吗?我怎样才能做到这一点呢?
发布于 2020-12-17 01:28:43
我认为这可以通过replyOptions中的onResponse处理程序来完成
server.register(require('fastify-http-proxy'), {
upstream: 'http://my-legacy-webserver.com',
prefix: '/legacy',
replyOptions: {
onResponse (reply) {
// you have access to the response here, e.g. check for errors and handle them
reply.send("your modified response");
}
}
})https://stackoverflow.com/questions/65324252
复制相似问题