2
\$\begingroup\$

I'm creating a Node.js app that allows the user to create their own social network. This code doesn't do anything special yet except creating a basic server and making it available on the Internet via a persistent address. As the address can be assigned to any person at any time, I make the server check for available addresses in the form of [device uuid]-[discriminator], where the discriminator is any number between 0 and 29. So the client then connect to the server by probing 30 possible addresses (sounds awful ;P) until the correct address is found. Anyway here is the code, please review it for me, I want this thing to be as perfect as possible.

Server

const localtunnel = require("localtunnel");
const portscanner = require("portscanner");
const express = require("express");
const low = require("lowdb");
const FileSync = require("lowdb/adapters/FileSync");
const uuidv4 = require("uuid/v4");

const getSubdomainFromURL = url => new URL(url).hostname.split(".")[0];
const constructSubdomain = (uuid, discriminator) => uuid + "-" + discriminator;
const createTunnel = (subdomain, port) => new Promise((resolve, reject) =>
  localtunnel(port, { subdomain: subdomain }, (error, tunnel) => {
    if (error) {
      reject(error);
    } else if (getSubdomainFromURL(tunnel.url) === subdomain) {
      resolve(tunnel);
    } else {
      tunnel.close();
      reject(tunnel);
    }
  })
);

const adapter = new FileSync("db.json");
const db = low(adapter);
db
  .defaults({
    deviceUUID: uuidv4()
  })
  .write();

const defaultPorts = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020];
portscanner.findAPortNotInUse(defaultPorts)
  .then(async port => {
    const numberOfAttempts = 30;
    let subdomain;
    for (let i = 0; i < numberOfAttempts; i++) {
      try {
        subdomain = constructSubdomain(db.get("deviceUUID"), i);
        await createTunnel(subdomain, port);
        break;
      } catch (error) {
        console.error(error);
        if (i === numberOfAttempts - 1) {
          console.log("Sorry, we are giving up. We can't create a tunnel for you after " + numberOfAttempts + " unsuccessful attempts.");
          process.exit();
        }
        continue;
      }
    }
    console.log("Here is your subdomain: " + subdomain);
    const app = express();
    app.get("/", (req, res) => res.send("LOCAL_GROUP_NETWORK_INSTANCE"));
    app.listen(port, () => console.log("Application server listening on port " + port));
  });

Client

const axios = require("axios");
const numberOfAttempts = 30;
const deviceID = process.argv[2];
const constructURL = (deviceID, discriminator) => `https://${deviceID}-${discriminator}.localtunnel.me/`;
const request = (deviceID, i = 0) => {
  console.log(`Establishing connection: ${i + 1}/30`);
  if (i === 30) return console.log("Finally giving up...");
  axios.get(constructURL(deviceID, i), { timeout: 3000 })
    .then(response =>
      response === "LOCAL_GROUP_NETWORK_INSTANCE"
        ? request(deviceID, i + 1)
        : console.log("We are the world!")
    )
    .catch(error => request(deviceID, i + 1))
};
request(deviceID);

I know the way my app works sound ridiculous but it's just an experiment, I don't really care much about it. What I need is a code review.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.