Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy4groovy/dea515d3ca72333973b3 to your computer and use it in GitHub Desktop.
Save crazy4groovy/dea515d3ca72333973b3 to your computer and use it in GitHub Desktop.
Simple NodeJS file (stream) proxy for local system files
var http = require('http'),
fs = require('fs'),
port = process.argv[2] || '5000';
var m_names =
["January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December"];
var server = http.createServer(function (req, res) {
var d = new Date(),
curr_day = d.getDate(),
curr_month = d.getMonth(),
curr_year = d.getFullYear(),
full_date = curr_day + "-" + m_names[curr_month] + "-" + curr_year;
try {
req.url = req.url.substr(1) ? decodeURIComponent(req.url).substr(1) : process.argv[1];
fs.exists(req.url, function (exists) {
exists ? console.log(full_date + " >> " + req.url) : 0;
exists ? fs.createReadStream(req.url).pipe(res) : res.end();
});
}
catch(ignore) { }
});
server.listen(port);
console.log('http://localhost:'+port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment