public
Last active — forked from igrigorik/webapp.rb

node.js version inspired by igrigorik's any ruby object, as a webapp! 'cause we can too.

  • Download Gist
README.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/usr/bin/env node
 
var http = require('http')
, webapp = require('webapp');
 
http.createServer(webapp.bind([])).listen(8000);
// ^^^^^^^^^^^^^^^
// | (x)
// ROFLSCALE DEQUE ---/
 
// http://localhost:8000/push/1 -> 1
// http://localhost:8000/push/2/3 -> 3
// http://localhost:8000/unshift/4 -> 4
 
// http://localhost:8000/ -> [ "4", "1", "2", "3" ]
 
// http://localhost:8000/pop -> "3"
// http://localhost:8000/shift -> "4"
 
function Thing() { }
Thing.prototype.set = function(k, v) { return this[k] = v; };
Thing.prototype.get = function(k) { return this[k]; };
http.createServer(webapp.bind(new Thing())).listen(8001);
// ^^^^^^^^^^^^^^^^^^^^^^^^
// | (x)
// ROFLSCALE NOSQL DB ---/
 
// http://localhost:8001/set/name/joe -> "joe"
// http://localhost:8001/set/age/30 -> "30"
 
// http://localhost:8001/ -> {"name":"joe","age":"30"}
 
// http://localhost:8001/get/age -> "30"
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
{
"name": "webapp",
"version": "0.0.2",
"author": "visnup <visnupx@gmail.com> (http://visnup.com)",
"main": "./webapp.js",
"engines": {
"node": "*"
},
"directories": {
"lib": "."
},
"files": [
""
],
"description": "Make a webapp out of anything",
"homepage": "https://gist.github.com/700995",
"repository": {
"type": "git",
"url": "git@gist.github.com:700995.git"
}
}
webapp.js
JavaScript
1 2 3 4 5 6 7
module.exports = function webapp(req, res) {
var s = 200, path = req.url.split('/'), r;
try { r = path[1] === '' ? this : this[path[1]].apply(this, path.slice(2)); }
catch(e) { s = 500; r = e; }
res.writeHead(s, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(r));
};

It's missing the roflcopter! :)

brought back by popular demand!

fyi, roflscale = ~500 pushes before "writes" get slow..

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.