Skip to content

Commit

Permalink
completed basic usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdul Moiz Hussain committed Oct 21, 2023
1 parent 90c8739 commit 870bf20
Show file tree
Hide file tree
Showing 4 changed files with 597 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bower_components

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
build

# Dependency directories
node_modules/
Expand Down
112 changes: 112 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

const chalk = require("chalk");
const httpServer = require("http-server");
const os = require("os");

/*
Starting up http-server, serving ./
http-server settings:
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: false
Default File Extension: none
Available on:
http://192.168.56.1:8080
http://192.168.1.109:8080
http://127.0.0.1:8080
Hit CTRL-C to stop the server
*/

const portInt = parseInt(process.argv[2]);

const port = isNaN(portInt) ? 99 : portInt;
const gzip = true;
const https = false;
const root = "./";

function logErrorAndHold(error) {
console.log(error);
require('readline')
.createInterface(process.stdin, process.stdout)
.question("Press [Enter] to exit...", function () {
process.exit();
});
}

// https://stackoverflow.com/a/29362887/8075004
process.on('uncaughtException', logErrorAndHold);

mainApp().catch(logErrorAndHold);

function requestLogger(req, res, error) {
const date = new Date().toJSON();
// var ip = argv['log-ip'] ? req.headers['x-forwarded-for'] || '' + req.connection.remoteAddress : '';
const ip = req.headers['x-forwarded-for'] || '' + req.connection.remoteAddress;
if (error) {
console.log(
'[%s] %s "%s %s" Error (%s): "%s"',
date, ip, chalk.red(req.method), chalk.red(req.url),
chalk.red(error.status.toString()), chalk.red(error.message)
);
}
else {
console.log(
'[%s] %s "%s %s" "%s"',
date, ip, chalk.cyan(req.method), chalk.cyan(req.url),
req.headers['user-agent']
);
}
}

async function mainApp() {

const server = httpServer.createServer({
root: root,
logFn: requestLogger,
https: https,
cors: true,
gzip: gzip,
cache: 2,
before: ""
});

console.log("%s %s",
chalk.yellow(`Starting up http-server, serving `), chalk.cyan(`${root}\n`));

try {
server.listen(port, () => {
const obj = os.networkInterfaces();
const networks = [];
const protocolStr = https ? "https://" : "http://";
for (const key of Object.keys(obj)) {
const valuesArray = obj[key];
valuesArray.forEach(v => {
if (v.family === "IPv4") {
const line = `${protocolStr}${v.address}:${port}`;
networks.push({ url: line, key: key });
}
});
}

console.log(chalk.yellow(`Available on:`));

const lengthOfLongestURL = Math.max(...networks.map(x => x.url.length));
networks.forEach(x => {
console.log(" " + x.url.padEnd(lengthOfLongestURL, " ") + ` ${x.key}`);
});

console.log(`Hit CTRL - C to stop the server\n`);
});
}
catch (eerr) {
console.log(eerr);
}
}

Loading

0 comments on commit 870bf20

Please sign in to comment.