问题:
我有一个 Ember CLI 应用程序,它将使用多个 API,我需要在开发模式下代理这些 API。
背景:
我有一个遗留的 api,它在以下位置公开服务/api
在我的本地开发机器上运行localhost:3000
我有一个新的 api,它在以下位置公开服务/myapp/api/v1
。这些服务最近从旧应用程序中提取,包含 ember 应用程序使用的大部分应用程序服务。
ember 应用程序使用的 baseURL 为/myapp
,因为它被部署到子目录中。
我使用生成了两个http代理ember generate http-proxy
。他们位于/server/proxies/api.js
and server/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/whatever)失败。
它似乎没有被使用或考虑。当我运行时,例如向 myapp/api/v1/sessions 发送 POST,它只是说无法 POST。当我将调试器放在 proxy.on 和 app.use 函数上时,它们永远不会被命中。
我这里哪里出错了?