Last active
August 29, 2015 13:56
-
-
Save addisaden/9089579 to your computer and use it in GitHub Desktop.
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 fs = require("fs"); | |
var http = require("http"); | |
http.createServer(function (req, res) { | |
var current_path = "." + req.url; | |
if(req.url === "/") current_path = "./index.html"; | |
fs.exists(current_path, function(exists) { | |
if(exists) { | |
fs.stat(current_path, function(err, stats) { | |
if(stats.isFile()) { | |
fs.createReadStream(current_path).pipe(res); | |
} else { | |
res.writeHead(200, {"Content-Type": "text/plain"}); | |
res.end("Path is not a file."); | |
} | |
}); | |
} else { | |
res.writeHead(200, {"Content-Type": "text/plain"}); | |
res.end("Path does not exists."); | |
} | |
}); | |
}).listen(3000); | |
console.log("Server is listening on port 3000"); | |
console.log("Server has content of " + process.cwd()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment