Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add echo and fileserver for node benchmark

  • Loading branch information...
commit 8f04b955b083bfcee9af102f44fda0e7c611bf69 1 parent f71c34b
Mark Wotton authored

Showing 1 changed file with 42 additions and 5 deletions. Show diff stats Hide diff stats

  1. 47  node-bench/pong.js
47  node-bench/pong.js
... ...
@@ -1,6 +1,43 @@
1  
-var http = require('http');
2  
-http.createServer(function (req, res) {
3  
-  res.writeHead(200, {'Content-Type': 'text/plain'});
4  
-  res.end('PONG');
  1
+var http = require("http"),  
  2
+url = require("url"),  
  3
+path = require("path"),  
  4
+fs = require("fs"),
  5
+sys = require('sys');
  6
+
  7
+
  8
+
  9
+http.createServer(function (request, response) {
  10
+
  11
+  var uri = url.parse(request.url).pathname;  
  12
+
  13
+  if(uri=='/pong') {
  14
+    response.writeHead(200, {'Content-Type': 'text/plain'});
  15
+    response.end('PONG');
  16
+  } else if ((match = uri.match(/^\/echo\/(.*)$/)) != null) {
  17
+    response.writeHead(200, {'Content-Type': 'text/plain'});
  18
+    response.end(match[1]);
  19
+  } else {
  20
+    var filename = path.join(process.cwd(), uri);  
  21
+    path.exists(filename, function(exists) {  
  22
+      if(!exists) {  
  23
+        response.writeHead(404, {"Content-Type": "text/plain"});  
  24
+        response.end("404 Not Found\n");  
  25
+        return;  
  26
+      }  
  27
+      
  28
+      fs.readFile(filename, "binary", function(err, file) {  
  29
+        if(err) {  
  30
+          response.writeHead(500, {"Content-Type": "text/plain"});  
  31
+          response.end(err + "\n");  
  32
+          return;  
  33
+        }  
  34
+  
  35
+        response.writeHead(200);  
  36
+        response.write(file, "binary");  
  37
+        response.end();  
  38
+      });  
  39
+    });  
  40
+  }
5 41
 }).listen(8124, "localhost");
6  
-console.log('Server running at http://127.0.0.1:8124/');
  42
+
  43
+sys.puts('Server running at http://127.0.0.1:8124/');

0 notes on commit 8f04b95

Please sign in to comment.
Something went wrong with that request. Please try again.