Ember 具有多个代理的 CLI

Ember CLI with Multiple Proxies

问题:

我有一个 Ember CLI 应用程序,它将使用多个 APIs,我需要在开发模式下代理它。

背景:

我有一个遗产 api,它在 /api 运行ning 在我位于 localhost:3000

的本地开发机器上公开服务

我有一个新的 api,它在 /myapp/api/v1 公开服务。这些服务最近从遗留应用程序中提取出来,构成了 ember 应用程序使用的大部分应用程序服务。

ember 应用程序使用 /myapp 的 baseURL,因为它正在部署到子目录。

我使用 ember generate http-proxy 生成了两个 http-proxy。它们位于 /server/proxies/api.jsserver/proxies/myapp/api/v1.js

api.js

var proxyPath = '/api';
module.exports = function(app) {
  var proxy = require('http-proxy').createProxyServer({});
  proxy.on('error', function(err, req) {
    console.error(err, req.url);
  });
  app.use(proxyPath, function(req, res, next){
    // include root path in proxied request
    req.url = proxyPath + '/' + req.url;
    proxy.web(req, res, { target: 'http://localhost:3000' });
  });
};

myapp/api/v1.js

var proxyPath = 'myapp/api/v1';
module.exports = function(app) {
  var proxy = require('http-proxy').createProxyServer({});
  proxy.on('error', function(err, req) {
    console.error(err, req.url);
  });
  app.use(proxyPath, function(req, res, next){
    req.url = proxyPath + '/' + req.url;
    proxy.web(req, res, { target: 'http://localhost:4100' });
  });
};

第一个代理,到 /api,似乎工作正常,第二个 API,到 /myapp/api/v1/ 失败。

它似乎没有被使用或考虑过。当我 运行 时,例如 POST 到 myapp/api/v1/sessions,它只是说不能 POST。当我将调试器放在 proxy.on 和 app.use 函数上时,它们永远不会被命中。

我哪里错了?

var proxyPath = 'myapp/api/v1';

您在字符串的开头缺少 / ;)