Hello,
I’m currently working on an entry-point proxy server with a single domain (like domain.glitch.me
) that will redirect requests to different service servers (with different domain names like domain-cluster1.glitch.me
).
It also works as a status page to display a maintenance page instead of the failed to start application
page of Glitch in case I’m doing some maintenance or the cluster application crash.
The whole thing seems to be working fine but sometimes, the request returns a 404 You found a glitch
page of the entry point domain.glitch.me
. This also happens with public files and scripts that are sometimes delivered, and sometimes aren’t.
Here’s the proxy server code hosted with my entry-point domain :
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const fs = require('fs')
const maintenanceMode = false
function toString(circ){
// Note: cache should not be re-used by repeated calls to JSON.stringify.
var cache = [];
return JSON.stringify(circ, (key, value) => {
if (typeof value === 'object' && value !== null) {
// Duplicate reference found, discard key
if (cache.includes(value)) return;
// Store value in our collection
cache.push(value);
}
return value;
});
}
app.use('*', createProxyMiddleware({
target: 'https://domain-cluster1.glitch.me',
changeOrigin: true,
proxyTimeout: 3000,
logLevel: 'silent',
onError: function(err, req, res, target) {
fs.readFile('./src/maintenance.html', 'utf-8', function(err, data){
res.writeHead(500, {
'Content-Type': 'text/html; charset=utf-8'
});
res.end(data);
}) // failed to start application on
},
onProxyRes: function(proxyRes, req, res) {
// res.end(toString(req))
if(proxyRes.statusCode == 503 || maintenanceMode){
let data = fs.readFileSync('./src/maintenance.html', 'utf-8')
res.writeHead(500, {
'Content-Type': 'text/html; charset=utf-8'
});
res.end(data);
}
}
}));
app.listen(8080);
Thanks for your help !