node.js - 使用反向代理提供公共(public)文件

标签 node.js hapi.js

Node.js 8.9.1、hapi 16.6.2、h2o2 5.2.0

有一个reverse proxy使用外部 API 的路线。

  {
    method: '*',
    path: '/api/v2/{param*}',
    handler: {
      proxy: {
        host: 'host.net',
        port: 8100,
        protocol: 'http',
        passThrough: true,
        localStatePassThrough: true
      }   
    }   
  } 

我需要在同一个 Node.js 服务器上提供 AngulaJS UI。添加以下路线。

  {
    method: 'GET',
    path: '/{param*}',
    handler: {
      directory: {
        path: 'public'
      }   
    }   
  }

现在我看到了用户界面。但我无法再从外部 API 获取数据

curl -XGET localhost:8001/api/v2/birds
{"statusCode":404,"error":"Not Found","message":"Not Found"}

如何在同一个 Node.js 服务器上同时提供 UI 和反向代理服务?

最佳答案

我通过使反向代理路由更加具体来使其工作。现在我有 3 个反向代理路由,而不是一个。

  {
    method: 'GET',
    path: '/api/v2/{param*}',
    handler: {
      proxy: {
        host: 'host.net',
        port: 8100,
        protocol: 'http',
        passThrough: true,
        localStatePassThrough: true
      }   
    }   
  },
  {
    method: 'POST',
    path: '/api/v2/{param*}',
    handler: {
      proxy: {
        host: 'host.net',
        port: 8100,
        protocol: 'http',
        passThrough: true,
        localStatePassThrough: true
      }   
    }   
  },
  {
    method: 'PUT',
    path: '/api/v2/{param*}',
    handler: {
      proxy: {
        host: 'host.net',
        port: 8100,
        protocol: 'http',
        passThrough: true,
        localStatePassThrough: true
      }   
    }   
  } 

关于node.js - 使用反向代理提供公共(public)文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47857788/

相关文章:

javascript - 防止 Node.js 控制台中的输入/输出混合?

node.js - 如何输出从 Hapi.js View 生成的 View 编译器错误?

javascript - 是否可以在 hapi 中定义全局基本路径

http - Node js上的http GET请求问题

javascript - 警告 : React. createElement : type is invalid -- bundle. js

javascript - Node/Electron 中的暗模式检测

javascript - 在剧作家的第一个浏览器实例之后保持登录 session

node.js - 查询请求时路径的结构是怎样的?

node.js - 从 hapi-js 路由自动生成 swagger.yaml

regex - 错误 : pattern must be a RegExp