Skip to content

Instantly share code, notes, and snippets.

@visnup
Forked from igrigorik/webapp.rb
Created November 15, 2010 21:36
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save visnup/700995 to your computer and use it in GitHub Desktop.
Save visnup/700995 to your computer and use it in GitHub Desktop.
node.js version inspired by igrigorik's any ruby object, as a webapp! 'cause we can too.
#!/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"
{
"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"
}
}
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));
};
@drnic
Copy link

drnic commented Nov 16, 2010

It's missing the roflcopter! :)

@visnup
Copy link
Author

visnup commented Nov 16, 2010

brought back by popular demand!

@visnup
Copy link
Author

visnup commented Nov 16, 2010

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment