Skip to content

Instantly share code, notes, and snippets.

@Ajnasz
Last active October 28, 2015 20:44
Show Gist options
  • Save Ajnasz/10807c5c0987f9c549f2 to your computer and use it in GitHub Desktop.
Save Ajnasz/10807c5c0987f9c549f2 to your computer and use it in GitHub Desktop.
/*jshint node:true*/
/*eslint-env node*/
var fs = require('fs');
var url = require('url');
var mime = require('mime');
var path = require('path');
function serveError(code, res) {
'use strict';
res.writeHead(code);
res.end(String(500));
}
function serve404(res) {
'use strict';
serveError(404, res);
}
function serve500(res) {
'use strict';
serveError(500, res);
}
function serveDir(dirPath, res) {
'use strict';
console.log('serve %s', dirPath);
fs.readdir(dirPath, function (err, files) {
if (err) {
serve500(res);
return;
}
res.setHeader('Content-Type', 'text/html');
res.write('<!doctype html>');
res.write('<html>');
res.write('<head>');
res.write('<title>' + dirPath + '</title>');
res.write('<link rel="stylesheet" ' +
'href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" ' +
'integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" ' +
'crossorigin="anonymous">');
res.write('</head>');
res.write('<body>');
res.write('<div class="container">');
res.write('<h1>' + dirPath + '</h1>');
res.write('<ul>');
if (dirPath !== './') {
res.write('<li><a href="../">..</a></li>');
}
files.sort().forEach(function (f) {
var fPath = path.join(dirPath, f);
res.write('<li>');
res.write('<a href="/' + fPath + '">');
if (fs.statSync(fPath).isDirectory()) {
res.write('<span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span> ');
res.write(f);
} else {
res.write('<span class="glyphicon glyphicon-file" aria-hidden="true"></span> ');
res.write(f);
}
res.write('</a>');
res.write('</li>');
});
res.write('</ul>');
res.write('</div>');
res.write('</body>');
res.write('</html>');
res.end();
});
}
function serveFile(filePath, res) {
'use strict';
fs.stat(filePath, function (err, stats) {
if (err) {
serve404(res);
return;
}
console.log('serve %s', filePath);
var mimeType = mime.lookup(filePath);
fs.readFile(filePath, function (err, data) {
if (err) {
res.writeHead(404);
res.end();
console.log('error', err);
return;
}
console.log('serving', filePath);
res.writeHead(200, {
'Content-Type': mimeType,
'Last-Modified': stats.mtime,
'Content-Length': data.length
});
res.write(data);
res.end();
});
});
}
var port = process.env.PORT || 22334;
var host = process.env.HOST || 'localhost';
require('http').createServer(function (req, res) {
'use strict';
var urlObj = url.parse(req.url);
var fn = path.normalize('.' + urlObj.pathname);
fs.stat(fn, function (err, stats) {
if (err) {
console.log('fn', fn, err);
serve404(res);
} else {
if (stats.isFile()) {
serveFile(fn, res);
} else if (stats.isDirectory()) {
serveDir(fn, res);
}
}
});
}).listen(port, host);
console.log('listening on: %s:%d', host, port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment