【问题标题】:How to intercept response with fastify-http-proxy如何使用 fastify-http-proxy 拦截响应
【发布时间】:2020-12-17 01:28:43
【问题描述】:

我正在尝试利用 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,而旧版 Web 服务器会呈现其代理给客户端的自定义 404 页面。 我想拦截 404(可能是每 40 次和 50 次)响应并在我的 fastify 服务器中处理它们。有可能吗?我怎样才能做到这一点?

【问题讨论】:

    标签: node.js fastify


    【解决方案1】:

    我认为这可以通过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");
              }
          }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-30
      • 2016-03-28
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多