【发布时间】:2015-07-27 20:53:58
【问题描述】:
问题:
我有一个 Ember CLI 应用程序,它将使用多个 API,我需要在开发模式下代理。
背景:
我有一个旧版 api,它在 /api 公开服务,在我的本地开发机器 localhost:3000 上运行
我有一个新的 API,它在 /myapp/api/v1 公开服务。这些服务是最近从旧版应用程序中提取的,并且包含 ember 应用程序使用的大部分应用程序服务。
ember 应用程序使用/myapp 的 baseURL,因为它被部署到子目录。
我使用 ember generate http-proxy 生成了两个 http 代理。它们位于/server/proxies/api.js 和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 的第一个代理似乎正在工作,/myapp/api/v1/whatever 的第二个 API 失败。
它似乎没有被使用或考虑。当我运行时,例如对 myapp/api/v1/sessions 的 POST,它只是说不能 POST。当我将调试器放在 proxy.on 和 app.use 函数上时,它们永远不会被命中。
我哪里错了?
【问题讨论】: