Commits

Magnus Hoff committed 9bbd2b0

Added CRUD-example that implements R and D. C and U pending... Made dispatchToResource give the resource as the this-argument

Comments (0)

Files changed (4)

 resource-tree
 =============
 
-A useful separation of lookup (URL parsing) and resources for implementing a HTTP server.
+A useful separation of lookup (URL parsing/routing) and resources for implementing a HTTP server.
 
 Installation: `npm install resource-tree`
 
     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.
+Examples by use-case:
+
+ * I want to read a simple example: [example-fileresource.js][1]
+ * I want to serve a file directly from disk: [example-fileresource.js][1]
+ * I want to create a simple custom resource: [example-custom-resource.js][2]
+ * I want to implement CRUD: [example-crud.js][3]
 
 [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
+[3]: https://bitbucket.org/maghoff/resource-tree/src/tip/example-crud.js
+#!/usr/bin/env node
+
+var resource_tree = require('./resource-tree');
+
+
+var db = [
+	{
+		"name": "test.txt",
+		"Content-Type": "text/plain",
+		"body": "Test content"
+	}
+];
+
+
+function ListResource(dataRoot) {
+	resource_tree.Resource.call(this);
+
+	this.dataRoot = dataRoot;
+}
+
+ListResource.prototype = new resource_tree.Resource();
+ListResource.constructor = ListResource;
+
+ListResource.prototype.http_GET = function (req, res) {
+	res.writeHead(200, {"Content-Type": "text/html;charset=utf-8"});
+
+	res.write("<!DOCTYPE html>");
+	res.write("<html>");
+	res.write("<head>");
+	res.write("<title>CRUD</title>");
+	res.write("<script>");
+	res.write("function xhr(method, url) { var x = new XMLHttpRequest(); x.open(method,url,true); x.send(); }\n");
+	res.write("function del(d) { xhr('DELETE', d); }\n");
+	res.write("</script>");
+	res.write("</head>");
+	res.write("<body><ul>");
+	console.log(this);
+	for (var d in db) {
+		var url = this.dataRoot + d;
+		res.write('<li><a href="' + url + '">' + db[d].name + '</a> <div class="put"></div> <a href="javascript:del(\'' + url + '\')">[del]</a></li>');
+	}
+	res.write("</ul>");
+	res.write("</body>");
+	res.end("</html>");
+};
+
+
+function DataResource(id) {
+	resource_tree.Resource.call(this);
+
+	this.http_GET = function (req, res) {
+		var data = db[id];
+		res.writeHead(200, {"Content-Type": data["Content-Type"]});
+		res.end(data.body);
+	};
+
+	this.http_DELETE = function (req, res) {
+		delete db[id];
+		res.writeHead(204);
+		res.end();
+	};
+}
+
+DataResource.prototype = new resource_tree.Resource();
+DataResource.constructor = DataResource;
+
+
+function DataLookup() {
+	resource_tree.Lookup.call(this);
+}
+
+DataLookup.prototype = new resource_tree.Lookup();
+DataLookup.constructor = DataLookup;
+
+DataLookup.prototype.lookup = function (path, callback) {
+	var id = path.substr(1);
+	if (db.hasOwnProperty(id)) callback(new DataResource(id));
+	else callback(null);
+}
+
+
+var root = {
+    '/': new ListResource('data/'),
+    'data': new DataLookup()
+};
+
+resource_tree.createServer(root).listen(8080, "127.0.0.1");
+
+console.log("http://127.0.0.1:8080/");
 {
   "name": "resource-tree",
   "version": "0.0.0-4",
-  "description": "A useful separation of lookup (URL parsing) and resources for implementing a HTTP server.",
+  "description": "A useful separation of lookup (URL parsing/routing) and resources for implementing a HTTP server.",
   "main": "resource-tree.js",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   "keywords": [
     "http",
-    "resource"
+    "resource",
+    "routing",
+    "router"
   ],
   "author": "Magnus Hoff <maghoff@gmail.com>",
   "license": "BSD",
     var functionName = 'http_' + req.method;
     var functionObject = resource[functionName];
 
-    if (typeof functionObject === 'function') functionObject(req, res);
+    if (typeof functionObject === 'function') functionObject.call(resource, req, res);
     else methodNotAllowed(res, resource);
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.