Skip to content

Instantly share code, notes, and snippets.

@saml
Created June 22, 2016 16:22
Show Gist options
  • Save saml/c721ee757a8542db2f880a1d60d61724 to your computer and use it in GitHub Desktop.
Save saml/c721ee757a8542db2f880a1d60d61724 to your computer and use it in GitHub Desktop.
var http = require('http');
var fs = require('fs');
var usage = `Usage: ${process.argv[0]} ${process.argv[1]} <filepath> <mimetype> [port]`;
var filePath = process.argv[2];
if (!filePath) {
throw new Error(usage);
}
var mimetype = process.argv[3]
if (!mimetype) {
throw new Error(usage);
}
var port = parseInt(process.argv[4], 10) || 3001;
console.log(`Listening http://localhost:${port}`);
http.createServer(function(req, res) {
var stat = fs.statSync(filePath);
res.writeHead(200, {
'Content-Type': mimetype,
'Content-Length': stat.size
});
fs.createReadStream(filePath).pipe(res);
}).listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment