-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "webapp", | |
"version": "0.0.2", | |
"author": "visnup <[email protected]> (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": "[email protected]:700995.git" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}; |
brought back by popular demand!
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
It's missing the roflcopter! :)