简体   繁体   English

如何使用免费的Dyno在Heroku上使用NodeJS代理?

[英]How to proxy with NodeJS on Heroku with free Dyno?

I have a master server on Heroku which handles redirects to other 2 apps. 我在Heroku上有一个主服务器,它处理重定向到其他2个应用程序。 Basically there are 3 servers: 基本上有3台服务器:
- master server (handles redirects) - 主服务器(处理重定向)
- main app server - 主应用服务器
- secondary app server - 辅助应用服务器

The code below works great for redirect, but I have a problem with the sleep of servers with free dyno. 下面的代码非常适合重定向,但我对使用免费dyno的服务器的睡眠有问题。 When an user tries to access the secondary app, initially it wakes up the master server. 当用户尝试访问辅助应用程序时,最初它会唤醒主服务器。 When the master server wakes up, it tries to wake up the secondary app. 当主服务器唤醒时,它会尝试唤醒辅助应用程序。
For some reason it throws an error ("Application error"), but if you refresh the page, it serves the secondary app instantly. 由于某种原因,它会引发错误(“应用程序错误”),但如果您刷新页面,它会立即为辅助应用程序提供服务。

What should I do to avoid the error message? 我该怎么做才能避免错误信息?

const express = require('express');
const proxy = require('http-proxy-middleware');

const app = express();

const port = process.env.PORT || 3000;
const router = {
    'secondaryapp.example.com': 'https://secondaryapp.herokuapp.com'
};

app.use('/', proxy({
    target: 'https://mainapp.herokuapp.com',
    changeOrigin: true,
    router: router
}));

app.listen(port);

I'm not sure if there's something you can do to avoid this error, but at least you can catch it and retry the request multiple if needed, allowing the dyno to properly wake up. 我不确定你是否可以做些什么来避免这个错误,但至少你可以抓住它并在需要时重试请求,允许dyno正确唤醒。

What's the error code returned on the first request? 第一次请求时返回的错误代码是什么?

Is there any helpful trace when you run heroku logs --tail --app mainapp/secondaryapp ? 运行heroku logs --tail --app mainapp/secondaryapp时是否有任何有用的跟踪heroku logs --tail --app mainapp/secondaryapp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM