Ember CLI with Multiple Proxies

一曲冷凌霜 提交于 2019-12-09 10:16:53

问题


The Problem:

I have an Ember CLI Application that will consume multiple APIs, which I need to proxy to in development mode.

Background:

I have a legacy api which exposes services at /api running on my local development machine at localhost:3000

I have a new api which exposes services at /myapp/api/v1. These services were recently extracted from the legacy app, and comprises the majority of the application services used by the ember app.

The ember app uses the baseURL of /myapp, as it is being deployed to a subdirectory.

I generated two http-proxys using ember generate http-proxy. They are located at /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' });
  });
};

The first proxy, to /api, appears to be working, the second API, to /myapp/api/v1/whatever fails.

It doesn't appear to be used or considered. When I run , for instance a POST to myapp/api/v1/sessions, it just says cannot POST. When I put debugger on the proxy.on and app.use functions, they are never hit.

Where am I going wrong here?


回答1:


var proxyPath = 'myapp/api/v1';

You're missing a / in the beginning of the string ;)



来源:https://stackoverflow.com/questions/30267849/ember-cli-with-multiple-proxies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!