Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add echo and fileserver for node benchmark

  • Loading branch information...
commit 8f04b955b083bfcee9af102f44fda0e7c611bf69 1 parent f71c34b
@mwotton mwotton authored
Showing with 42 additions and 5 deletions.
  1. +42 −5 node-bench/pong.js
View
47 node-bench/pong.js
@@ -1,6 +1,43 @@
-var http = require('http');
-http.createServer(function (req, res) {
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end('PONG');
+var http = require("http"),
+url = require("url"),
+path = require("path"),
+fs = require("fs"),
+sys = require('sys');
+
+
+
+http.createServer(function (request, response) {
+
+ var uri = url.parse(request.url).pathname;
+
+ if(uri=='/pong') {
+ response.writeHead(200, {'Content-Type': 'text/plain'});
+ response.end('PONG');
+ } else if ((match = uri.match(/^\/echo\/(.*)$/)) != null) {
+ response.writeHead(200, {'Content-Type': 'text/plain'});
+ response.end(match[1]);
+ } else {
+ var filename = path.join(process.cwd(), uri);
+ path.exists(filename, function(exists) {
+ if(!exists) {
+ response.writeHead(404, {"Content-Type": "text/plain"});
+ response.end("404 Not Found\n");
+ return;
+ }
+
+ fs.readFile(filename, "binary", function(err, file) {
+ if(err) {
+ response.writeHead(500, {"Content-Type": "text/plain"});
+ response.end(err + "\n");
+ return;
+ }
+
+ response.writeHead(200);
+ response.write(file, "binary");
+ response.end();
+ });
+ });
+ }
}).listen(8124, "localhost");
-console.log('Server running at http://127.0.0.1:8124/');
+
+sys.puts('Server running at http://127.0.0.1:8124/');
Please sign in to comment.
Something went wrong with that request. Please try again.