首页 > 解决方案 > 使用 Heroku 在另一个测功机上代理 API

问题描述

我的 API 在一个测功机上,我的前端在另一个测功机上。

我不确定如何访问 api dyno。

例子:

web: yarn --cwd web start -p $PORT
api: bundle exec rails s -p 3001

然后是代理:


const apiProxy = createProxyMiddleware({
    target: 'http://127.0.0.1:3001',
    changeOrigin: true,
    pathRewrite: { [`^/api/graphql`]: '' },
    secure: false,
});

export default function (req, res) {
    apiProxy(req, res, (result) => {
        if (result instanceof Error) {
            throw result;
        }

        throw new Error(`Request '${req.url}' is not proxied! We should never reach here!`);
    });
};

不过,这似乎不是那么直截了当。

标签: node.jsruby-on-railsheroku

解决方案


一旦 API 组件部署在 Web Dyno 上,您应该使用 Heroku 分配的主机,例如myapi.herokuapp.com.
然后调用的端点是https://myapi.herokuapp.com


推荐阅读