Skip to content

Commit

Permalink
fix: node-static module is broken; remove it
Browse files Browse the repository at this point in the history
we can do simple html serving ourselves for now
  • Loading branch information
dbushong committed Sep 13, 2017
1 parent 16ced3d commit dc3dfb2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
]
}
},
"dependencies": {
"node-static": "~0.7.7"
},
"dependencies": {},
"devDependencies": {
"eslint": "^1.0.0",
"eslint-config-groupon": "^2.0.0",
Expand Down
24 changes: 14 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

var http = require('http');
var parseUrl = require('url').parse;

var StaticServer = require('node-static').Server;
var path = require('path');
var fs = require('fs');

function echo(request, response) {
var data = {
Expand Down Expand Up @@ -36,16 +36,20 @@ function crash(response) {
response.socket.destroy();
}

function serveFromDisk(file, request, response) {
var listener = request.addListener('end', function serveFile() {
file.serve(request, response);
});
listener.resume();
function serveFromDisk(pathname, response) {
var safePath = pathname.replace(/\.\./g, '').replace(/^\/+/, '');
if (safePath === '') safePath = 'index.html';
var filePath = path.resolve(__dirname, 'public', safePath);
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
response.setHeader('Content-Type', 'text/html');
fs.createReadStream(filePath).pipe(response);
} else {
response.statusCode = 404;
response.end('File Not Found');
}
}

function createServer() {
var file = new StaticServer(__dirname + '/public');

return http.createServer(function handleRequest(request, response) {
var parsedUrl = parseUrl(request.url);
switch (parsedUrl.pathname) {
Expand All @@ -62,7 +66,7 @@ function createServer() {
return undefined;

default:
return serveFromDisk(file, request, response);
return serveFromDisk(parsedUrl.pathname, response);
}
});
}
Expand Down

0 comments on commit dc3dfb2

Please sign in to comment.