-
-
Save DTrejo/1564297 to your computer and use it in GitHub Desktop.
fixed :)
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 ip = '127.0.0.1'; | |
var port = 2000; | |
var www = process.cwd(); | |
var mimetypes = {'html': 'text/html', 'js': 'text/javascript', 'css': 'text/css', 'png': 'image/png', 'svg': 'image/svg'}; | |
var path = require('path'); | |
http.createServer(function (req, res) { | |
fs.realpath(path.join(www, req.url), function(err, realpath){ | |
if (err || realpath.substr(0, www.length+1) != www+'/') { | |
res.writeHead(404, {'Content-Type': 'text/plain'}); | |
return res.end('No such file exists, sorry!'); | |
} | |
var filename = realpath.split('/').pop(); | |
var extension = (filename.split('.').length > 1) | |
? filename.split('.').pop() | |
: 'txt'; | |
var mimetype = (mimetypes[extension] === undefined) | |
? 'application/octet-stream' | |
: mimetypes[extension]; | |
res.writeHead(200, {'Content-Type': mimetype}); | |
fs.createReadStream(realpath).pipe(res); //herein lies the problem | |
// commented this out :) | |
// res.end(); | |
}); | |
}).listen(port, ip); | |
console.log('Serving '+www+' at http://'+ip+':'+port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment