Created
June 22, 2016 16:22
-
-
Save saml/c721ee757a8542db2f880a1d60d61724 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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