Skip to content

Instantly share code, notes, and snippets.

@prameshbajra
Last active June 8, 2018 12:36
Show Gist options
  • Save prameshbajra/100850b8b5352ce1454e448036612dcd to your computer and use it in GitHub Desktop.
Save prameshbajra/100850b8b5352ce1454e448036612dcd to your computer and use it in GitHub Desktop.
Share files locally in very high speed.
const cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork();
cluster.on('exit', function(worker, code, signal) {
cluster.fork();
});
}
if (cluster.isWorker) {
const express = require('express');
const fs = require('fs');
const path = require('path');
const { lstatSync, readdirSync } = require('fs');
const os = require('os');
const { join } = require('path');
const ifaces = os.networkInterfaces();
const app = express();
let localIP = "";
// To get your local IP such that other can access the file via this ...
Object.keys(ifaces).forEach(function (ifname) {
let alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
return;
}
localIP = iface.address;
++alias;
});
});
app.get("/download/:fileName",(req , res) => {
const fileName = req.params.fileName;
const srcPath = `${__dirname}/uploads/${fileName}`;
// Find a way to encrypt file ... Not by writing ...
const readStream = fs.createReadStream(srcPath);
res.writeHead(200, {
'Content-Type': 'application/octet-stream'
});
readStream.pipe(res);
});
app.get("/allDownloads", (req, res) => {
let files = ["Mind the filenames, They are case sensitive ... -> "," ", "The files you can download are ::: "];
fs.readdirSync(`${__dirname}/uploads`).forEach(file => {
files.push(file);
})
res.send(files);
});
app.listen(process.env.PORT || 3000, localIP);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment