Last active
September 9, 2016 18:28
-
-
Save YodasWs/093b83cca409d1cc0801704126d49d0c to your computer and use it in GitHub Desktop.
A simplistic NodeJS HTTP server
This file contains 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'), | |
path = require('path'), | |
fs = require('fs'), | |
optional = require('optional'), | |
sass = optional('node-sass') | |
http.createServer(function(req, res) { | |
var fileName = req.url == '/' ? '/index.html' : decodeURIComponent(req.url), | |
filePath = __dirname + '/www' + fileName, | |
ext = fileName.lastIndexOf('.') > -1 ? fileName.substring(fileName.lastIndexOf('.') + 1) : 'html' | |
console.log('Requested ' + req.url) | |
fs.exists(filePath, function(exists) { | |
if (exists) switch (ext) { | |
case 'sass': | |
case 'scss': | |
var outFile = filePath.substring(0, filePath.lastIndexOf('.')) + '.css', | |
iFileStats, oFileStats, noFile = false | |
// Test for outFile | |
try { | |
fs.readFileSync(outFile) | |
} catch (e) { | |
// Create outFile | |
fs.writeFileSync(outFile, '') | |
noFile = true // but still need to compress SCSS | |
} | |
iFileStats = fs.statSync(filePath) | |
oFileStats = fs.statSync(outFile) | |
// Only Compress Updated File | |
if (noFile || iFileStats.mtime.getTime() > oFileStats.mtime.getTime()) sass.render({ | |
outputStyle:'compressed', | |
indentType:'tab', | |
outFile:outFile, | |
file:filePath | |
}, function(err, contents) { | |
if (!err) { | |
// Output Compressed CSS | |
res.writeHead(200, {'Content-Type': 'text/css'}) | |
res.end(contents.css) | |
// And save for future use | |
fs.writeFile(outFile, contents.css) | |
} else { | |
res.writeHead(500) | |
console.dir(err) | |
} | |
}); else { | |
// Compressed File is Up-to-date, Output It | |
fs.readFile(outFile, function(err, contents) { | |
if (!err) { | |
// Output Compressed CSS | |
res.writeHead(200, {'Content-Type': 'text/css'}) | |
res.end(contents) | |
} else { | |
res.writeHead(500) | |
console.dir(err) | |
} | |
}) | |
} | |
break; | |
default: | |
// Read the file | |
fs.readFile(filePath, function(err, contents) { | |
if (!err) { | |
// If there were no errors, send the content with the default 200 OK header | |
switch (ext) { | |
case 'json': | |
res.writeHead(200, {'Content-Type': 'application/json'}) | |
break; | |
case 'css': | |
res.writeHead(200, {'Content-Type': 'text/css'}) | |
break; | |
case 'js': | |
res.writeHead(200, {'Content-Type': 'text/javascript'}) | |
break; | |
case 'html': | |
default: | |
res.writeHead(200, {'Content-Type': 'text/html'}) | |
} | |
res.end(contents) | |
} else { | |
res.writeHead(500) | |
console.dir(err) | |
} | |
}) | |
} else { | |
res.writeHead(404, {'Content-Type': 'text/html'}) | |
res.end() | |
} | |
}) | |
}).listen(3020) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment