A Node.js server that create a tunnel to the Internet

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
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.
javascript node.js server
add a comment |Â
up vote
2
down vote
favorite
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.
javascript node.js server
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
javascript node.js server
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.
javascript node.js server
edited Jun 17 at 22:34
Jamalâ¦
30.1k11114225
30.1k11114225
asked Jun 10 at 6:06
continued-
563
563
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196210%2fa-node-js-server-that-create-a-tunnel-to-the-internet%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password