使用 Heroku 代理到另一个 dyno 上的 API

时间:2021-01-02 04:17:19

标签: node.js ruby-on-rails heroku

我的 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!`);
    });
};

不过似乎没有这么简单。

1 个答案:

答案 0 :(得分:0)

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

相关问题