Commits

Magnus Hoff committed 0ec8d45

Some minimal documentation and a minor refactoring to cater for it

Comments (0)

Files changed (7)

+syntax: glob
+node_modules
+example*.js
 
 A useful separation of lookup and resources for implementing a HTTP server.
 
+Small example:
+
+    var resource_tree = require('resource-tree');
+
+    var root = {
+        // Assuming "example.js" exists in the same directory as this script
+        'example.js': new resource_tree.FileResource(require('path').join(__dirname, 'example.js'))
+    };
+
+    resource_tree.createServer(root).listen(8080, "127.0.0.1");
+
+    console.log("Now you can GET http://127.0.0.1:8080/example.js");
+    console.log("Everything else is properly handled as 404 (Not Found) or 405 (Method Not Allowed)");
+
+See [example-fileresource.js][1] and [example-custom-resource.js][2] for slightly more.
+
+[1]: https://bitbucket.org/maghoff/resource-tree/src/tip/example-fileresource.js
+[2]: https://bitbucket.org/maghoff/resource-tree/src/tip/example-custom-resource.js

example-custom-resource.js

+#!/usr/bin/env node
+
+var resource_tree = require('./resource-tree');
+var path = require('path');
+var fs = require('fs');
+var highlight = require('highlight').Highlight; // npm install highlight
+
+
+function HighlightResource(fullpath) {
+    resource_tree.Resource.call(this);
+
+    this.http_GET = function (req, res) {
+        fs.readFile(fullpath, 'utf-8', function (err, data) {
+            if (err) {
+                res.writeHead(500, {'Content-Type': 'text/plain'});
+                res.end("Internal Server Error");
+                return;
+            }
+
+            res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
+            var style = ".keyword{font-weight:bold} .string{color:green} .comment{color:grey}";
+            res.write("<!DOCTYPE html>\n<html><head><title>" + fullpath + "</title><style>" + style + "</style></head><body><pre>");
+            res.write(highlight(data));
+            res.end("</pre></body></html>");
+        });
+    };
+}
+
+HighlightResource.prototype = new resource_tree.Resource();
+HighlightResource.constructor = HighlightResource;
+
+
+var root = {
+    'example-custom-resource.js': new resource_tree.FileResource(path.join(__dirname, 'example-custom-resource.js')),
+    'example-custom-resource.js.html': new HighlightResource(path.join(__dirname, 'example-custom-resource.js'))
+};
+
+resource_tree.createServer(root).listen(8080, "127.0.0.1");
+
+console.log("Now you can GET http://127.0.0.1:8080/example-custom-resource.js and http://127.0.0.1:8080/example-custom-resource.js.html");

example-fileresource.js

+#!/usr/bin/env node
+
+var resource_tree = require('./resource-tree');
+var http = require('http');
+var path = require('path');
+
+var root = {
+    'example-fileresource.js': new resource_tree.FileResource(path.join(__dirname, 'example-fileresource.js'))
+};
+
+var server = http.createServer(function (req, res) {
+    resource_tree.handleRequest(root, req, res);
+});
+
+/* The initialization of `server` can be shortened to either of the following:
+
+var server = http.createServer(resource_tree.handleRequestFunction(root));
+
+var server = resource_tree.createServer(root);
+
+*/
+
+server.listen(8080, "127.0.0.1");
+
+console.log("Now you can GET http://127.0.0.1:8080/example-fileresource.js");
+console.log("Everything else is properly handled as 404 (Not Found) or 405 (Method Not Allowed)");
 {
   "name": "resource-tree",
-  "version": "0.0.0-3",
-  "description": "A useful separation of lookup and resources for implementing a HTTP server.",
+  "version": "0.0.0-4",
+  "description": "A useful separation of lookup (URL-parsing) and resources for implementing a HTTP server.",
   "main": "resource-tree.js",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
     else methodNotAllowed(res, resource);
 }
 
-function createServer(root) {
-    return http.createServer(function (req, res) {
-        console.log('[req] ' + req.url);
+function handleRequest(root, req, res) {
+    console.log('[req] ' + req.url);
 
-        res.setHeader("Server", module.name + "/" + module.version);
+    res.setHeader("Server", module.name + "/" + module.version);
 
-        pathname = url.parse(req.url).pathname;
-        Lookup.prototype.doLookup(root, pathname, function(resource) {
-            if (resource === null) {
-                res.writeHead(404, {'Content-Type': 'text/plain'});
-                res.end('Not found\n');
-            } else {
-                dispatchToResource(req, res, resource);
-            }
-        });
+    pathname = url.parse(req.url).pathname;
+    Lookup.prototype.doLookup(root, pathname, function(resource) {
+        if (resource === null) {
+            res.writeHead(404, {'Content-Type': 'text/plain'});
+            res.end('Not found\n');
+        } else {
+            dispatchToResource(req, res, resource);
+        }
     });
 }
 
+function handleRequestFunction(root) {
+    return function (req, res) {
+        handleRequest(root, req, res);
+    };
+}
+
+function createServer(root) {
+    return http.createServer(handleRequestFunction(root));
+}
+
 
 exports.splitOneLevel = splitOneLevel;
+
 exports.Lookup = Lookup;
-exports.Resource = Resource;
 exports.MapLookup = MapLookup;
-exports.FileResource = FileResource;
 exports.FileLookup = FileLookup;
 exports.DirectLookup = DirectLookup;
 exports.OneLevelLookup = OneLevelLookup;
+
+exports.Resource = Resource;
+exports.FileResource = FileResource;
 exports.PermanentRedirectResource = PermanentRedirectResource;
+
+exports.handleRequest = handleRequest;
+exports.handleRequestFunction = handleRequestFunction;
 exports.createServer = createServer;