proxyTable

使用 Proxy Table 分发请求到不同地址:

  • Host HTTP header.
  • Request path
  • Host HTTP header + path
var express = require('express');
var proxyMiddleware = require("http-proxy-middleware");

var proxyTable = {
    "integration.localhost:3000" : "http://localhost:8001",    // host only
    "staging.localhost:3000"     : "http://localhost:8002",    // host only
    "localhost:3000/api"         : "http://localhost:8003",    // host + path
    "/rest"                      : "http://localhost:8004"     // path only
};

var options = {
    target: 'http://localhost:8000',
    proxyTable: proxyTable
};

var proxy = proxyMiddleware('/', options);

var app = express();
app.use(proxy);                      // add the proxy to express

app.listen(3000);

Example

上面示例,所有的请求被代理到 http://localhost:8000.

当请求的 Host HTTP header and/or path 匹配 proxyTable 的配置时,会被代理到目标地址。

http://localhost:3000/lorum/ipsum             -> http://localhost:8000/lorum/ipsum
http://integration.localhost:3000/lorum/ipsum -> http://localhost:8001/lorum/ipsum
http://staging.localhost:3000/rest/foo/bar    -> http://localhost:8002/rest/foo/bar
http://localhost:3000/api/houses/123          -> http://localhost:8003/api/houses/123
http://localhost:3000/rest/books/123          -> http://localhost:8004/rest/books/123

results matching ""

    No results matching ""