Setting up infinite NodeJS servers over HTTPS... For FREE

Hello :slight_smile:

Ok so behind this clickbait-like title, I actually want to share something nice with the community :grin:

I found a nice way to create as many NodeJS server as I want, (a new server being created within less than a minute) starting from an existing one.
Spoiler Alert : In fact it’s only one (paid) server in total of course, there is nothing FREE :wink:


So, here is the trick :


Now, here is the nice part :

  • On the main server, I have one folder for each child server. Each folder contains exactly the content of a “normal” NodeJS server : a main file main.js, and its dependencies (public/ folder, assets/, or anything…)
  • In the root folder, I have a very simple parent server :
// Requirements
const express = require('express');
const vhost = require('vhost');
const port = process.env.PORT;
const hover_app = require('./hover/main.js').hover_app;
const tchat_app = require('./tchat/main.js').tchat_app;
const game_app = require('./game/main.js').game_app;
const vr_app = require('./vr/main.js').vr_app;

// Express
const App = express();

// Forwarding depending on subdomain
App.use(vhost('hover.tricotools.com', hover_app));
App.use(vhost('tchat.tricotools.com', tchat_app));
App.use(vhost('game.tricotools.com', game_app));
App.use(vhost('vr.tricotools.com', vr_app));

// Listen
App.listen(port);

The above code redirects automatically each request and connexion toward the considered server, based on the subdomain URL the user is arriving from

On the child server side, I simply export an Express App :

// Express
const App = express();

// MiddleWares
App.use('middleware_01', params_01);
App.use('middleware_02', params_02);
App.use('middleware_03', params_03);

// Export this App
exports.hover_app = App;

And that’s it :slight_smile:


Is means that now when I have a “new idea”, and want to give it a try over a new “fresh” server (free of anybullshit from another game or whatever) I need to :

  • Copy a folder and rename it (5 seconds)
  • Add a new vhost line in the parent server (5 seconds)
  • Commit & Push (20 seconds)
  • Connect to AWS Route 53 and redirect this new name (30 seconds)

Total : 1 minute for a new server up and running, over HTTPS :grin:


Maybe all this bullshit sounds obvious for many of you, but on my side I’m quite happy with this new trick. The total RAM used is a few MB in total, the CPU is almost at 0% most of the time… What I mean is, unless I set up a multiplayer game with thousands of people, it does not add any “charge” to the bill :stuck_out_tongue:

++
Tricotou

2 Likes