Created — forked from igrigorik/webapp.rb

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

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

View README.js
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"
View README.js
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"
}
}
View README.js
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! :)

Owner

brought back by popular demand!

Owner

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
Something went wrong with that request. Please try again.