具有多个代理的Ember CLI [英] Ember CLI with Multiple Proxies

查看:61
本文介绍了具有多个代理的Ember CLI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

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

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

背景:

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

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

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

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.

ember应用程序使用<$ c $的baseURL c> / myapp ,因为它正被部署到子目录。

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

我使用 ember生成了两个http-proxy生成http-proxy 。它们位于 /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

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 /无论什么都会失败。

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

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

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.

我在哪里出错了?

推荐答案

var proxyPath = 'myapp/api/v1';

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

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

这篇关于具有多个代理的Ember CLI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆