Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simple web.js web server.

  • Loading branch information...
commit 3ada54a90897e719313d702cc8801f00e6cbd9e9 1 parent 03bd253
@steveyen steveyen authored
Showing with 46 additions and 3 deletions.
  1. +5 −0 body.tmpl
  2. +1 −3 cook.js
  3. +40 −0 web.js
View
5 body.tmpl
@@ -1 +1,6 @@
+<body>
<h1>hello world</h1>
+<script>
+</script>
+</body>
+
View
4 cook.js
@@ -5,9 +5,6 @@ var fs = require("fs");
var o = console.log
o("<html>");
-o("<body>")
-o(fs.readFileSync("./body.tmpl").toString());
-o("</body>")
o("<script>")
o(" var path_stat = {")
@@ -47,4 +44,5 @@ process.argv.slice(2).forEach(process_dir);
o(" };")
o("</script>")
+o(fs.readFileSync("./body.tmpl").toString());
o("</html>")
View
40 web.js
@@ -0,0 +1,40 @@
+#!/usr/bin/env node
+
+var http = require("http"),
+ url = require("url"),
+ path = require("path"),
+ fs = require("fs")
+ port = process.argv[2] || 8888;
+
+http.createServer(function(request, response) {
+ var uri = url.parse(request.url).pathname;
+ var filename = path.join(process.cwd(), uri);
+
+ 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;
+ }
+
+ response.writeHead(200);
+ response.write(file, "binary");
+ response.end();
+ });
+ });
+ }).listen(parseInt(port, 10));
+
+console.log("web.js - serving http://localhost:" + port);
Please sign in to comment.
Something went wrong with that request. Please try again.