Proxy on glitch

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 !

Hey Joprocorp,

I hoped someone else might be able to jump in and answer this because I look at it and think “I have no idea”!

I wonder how you can be sure that you’re getting the “You found a Glitch” error served from the root proxy (domain) and not from the sub-project that you’re directing to? I suppose your error handler would prevent that?

Do you have any logs that correspond to the times you get the error?

1 Like

Thank you for your response.

I hoped someone else might be able to jump in and answer this because I look at it and think “I have no idea”!

Well, that was what I was affraid of.

I wonder how you can be sure that you’re getting the “You found a Glitch” error served from the root proxy ( domain ) and not from the sub-project that you’re directing to? I suppose your error handler would prevent that?

I actually get it from time to time when I open a page, but how I made sure the error is coming from the requests from my main domain is looking at the Network tab.

polygenda.glitch.me/assets/img/logo.png 404

Sometimes, the logo returns with 404 page (the HTTP error code returned by the “You found a Glitch”, and sometimes it doesn’t.

And more importantly, I can’t reproduce this issue anymore ! It seems like I don’t have any 404 errors and there is no “You found a glitch” page popping out of nowhere now.

Maybe it was a glitch, quite literally. At the moment, I can’t give you information about the error anymore and I don’t have any logs (because I was receiving literally no requests on my end, it was caught by Glitch).

If it does happen again, I will make sure to retrieve the logs.
Thanks again.

1 Like