-
-
Save zazk/63c8c6fed2553e066da1 to your computer and use it in GitHub Desktop.
Node.JS static file web server. Put it in your path to fire up servers in any directory, takes an optional port argument.
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"), | |
url = require("url"), | |
path = require("path"), | |
dir = require('node-dir'), | |
swig = require('swig'), | |
fs = require("fs"), | |
SERVER = "server", | |
BUILD = "build", | |
SOURCE_FOLDER = "/source", | |
BUILD_FOLDER = "/build", | |
command = process.argv[2] || SERVER, | |
port = process.argv[3] || 8888 | |
; | |
console.log( "Params:", process.argv[2] ); | |
var deleteFolderRecursive = function(path) { | |
var files = []; | |
if( fs.existsSync(path) ) { | |
files = fs.readdirSync(path); | |
files.forEach(function(file,index){ | |
var curPath = path + "/" + file; | |
if(fs.lstatSync(curPath).isDirectory()) { // recurse | |
deleteFolderRecursive(curPath); | |
} else { // delete file | |
fs.unlinkSync(curPath); | |
} | |
}); | |
fs.rmdirSync(path); | |
} | |
}; | |
//Create Server | |
if ( command == SERVER ){ | |
http.createServer(function(request, response) { | |
var uri = url.parse(request.url).pathname | |
, filename = path.join(process.cwd(), SOURCE_FOLDER, uri); | |
console.log("File:", filename, "Uri", uri, "cwd",process.cwd()); | |
path.exists(filename, function(exists) { | |
if(!exists) { | |
response.writeHead(404, {"Content-Type": "text/plain"}); | |
response.write("404 Not Found\n"); | |
response.end(); | |
return; | |
} | |
if (fs.statSync(filename).isDirectory()) filename += '/index.html'; | |
fs.readFile(filename, "binary", function(err, file) { | |
if(err) { | |
response.writeHead(500, {"Content-Type": "text/plain"}); | |
response.write(err + "\n"); | |
response.end(); | |
return; | |
} | |
swig.setDefaults({ cache: false }); | |
response.writeHead(200); | |
response.write( swig.renderFile(filename), "binary"); | |
response.end(); | |
}); | |
}); | |
}).listen(parseInt(port, 10)); | |
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown"); | |
} | |
// Create Build | |
else if (command == BUILD ){ | |
console.log("Create Build"); | |
var folder = path.join(process.cwd(), BUILD_FOLDER); | |
//Create Build Folder | |
deleteFolderRecursive(folder); | |
fs.mkdir(folder); | |
// match only filenames with a .txt extension and that don't start with a `.´ | |
dir.readFiles(__dirname, { | |
match: /.html$/, | |
exclude: /^\./ | |
}, function(err, content, next) { | |
if (err) throw err; | |
//console.log('content:', content); | |
next(); | |
}, | |
function(err, files){ | |
if (err) throw err; | |
for (var i = 0; i<files.length; i++) { | |
var file = files[i], | |
dir = file.split("/").slice(0, -1).join("/").replace(SOURCE_FOLDER,BUILD_FOLDER) ; | |
if (!fs.existsSync(dir)) { | |
fs.mkdir(dir); | |
} | |
//console.log("File:", file, " Dir:", dir); | |
fs.writeFile(file.replace(SOURCE_FOLDER,BUILD_FOLDER), swig.renderFile(file), function(err) { | |
if(err) { | |
console.log(err); | |
} else { | |
console.log("The file was saved!", file); | |
} | |
}); | |
} | |
//console.log('finished reading files:',files); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment