Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simple static server

  • Loading branch information...
commit 021f40c5a606cfbd9c053bb0f02033a46901f16c 1 parent b629681
@hsbt authored
Showing with 27 additions and 4 deletions.
  1. +27 −4 app.js
View
31 app.js
@@ -1,10 +1,33 @@
var util = require('util'),
- http = require('http');
+ http = require('http'),
+ url = require('url'),
+ path = require('path'),
+ fs = require('fs');
http.createServer(function(req, res) {
- res.writeHead(200, {"Content-Type": "text/html"});
- res.write("Heelo World");
- res.end();
+ var uri = url.parse(req.url).pathname;
+ var filename = path.join(process.cwd(), uri);
+
+ path.exists(filename, function(exists) {
+ if(!exists) {
+ res.writeHead(404, {"Content-Type": "text/plain"});
+ res.write("404 Not Found\n");
+ res.end();
+ }
+
+ fs.readFile(filename, "binary", function(err, file) {
+ if(err) {
+ res.writeHead(500, {"Content-Type": "text/plain"});
+ res.write(err + "\n");
+ res.end();
+ return;
+ }
+
+ res.writeHead(200);
+ res.write(file, "binary");
+ res.end();
+ });
+ });
}).listen(8080);
util.puts("Server running at http://localhost:8080/");
Please sign in to comment.
Something went wrong with that request. Please try again.