问题:

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

背景:

我有一个旧版api,它以/api在本地开发计算机上运行的localhost:3000公开服务

我有一个新的api,它在/myapp/api/v1公开服务。这些服务是最近从旧版应用程序中提取的,并且包含了ember应用程序使用的大多数应用程序服务。

ember应用程序将/myapp的baseURL部署到子目录中。

我使用ember generate http-proxy生成了两个http代理。它们位于/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的第一个代理似乎可以正常工作,到/myapp/api/v1/的第二个API似乎都失败了。

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

我在哪里错了?

最佳答案

var proxyPath = 'myapp/api/v1';

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

关于proxy - 具有多个代理的Ember CLI,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30267849/

10-11 09:10
查看更多