How to proxy with NodeJS on Heroku with free Dyno?

Viewed 378

I have a master server on Heroku which handles redirects to other 2 apps. Basically there are 3 servers:
- 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. 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);
1 Answers

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.

What's the error code returned on the first request?

Is there any helpful trace when you run heroku logs --tail --app mainapp/secondaryapp?

Related