-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] refactor out middlewares from examples.
- Loading branch information
1 parent
020290a
commit 2cf4e0a
Showing
5 changed files
with
82 additions
and
136 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
module.exports = Store | ||
// | ||
// just to make these example a little bit interesting, | ||
// make a little key value store with an http interface | ||
// (see couchbd for a grown-up version of this) | ||
// | ||
// API: | ||
// GET / | ||
// retrive list of keys | ||
// | ||
// GET /[url] | ||
// retrive object stored at [url] | ||
// will respond with 404 if there is nothing stored at [url] | ||
// | ||
// POST /[url] | ||
// | ||
// JSON.parse the body and store it under [url] | ||
// will respond 400 (bad request) if body is not valid json. | ||
// | ||
// TODO: cached map-reduce views and auto-magic sharding. | ||
// | ||
|
||
|
||
|
||
function Store () { | ||
this.store = {} | ||
} | ||
Store.prototype = { | ||
get: function (key) { | ||
return this.store[key] | ||
}, | ||
set: function (key, value) { | ||
return this.store[key] = value | ||
}, | ||
handler:function () { | ||
var store = this | ||
return function (req, res) { | ||
function send (obj, status) { | ||
res.writeHead(200 || status,{'Content-Type': 'application/json'}) | ||
res.write(JSON.stringify(obj) + '\n') | ||
res.end() | ||
} | ||
var url = req.url.split('?').shift() | ||
if(url === '/') { | ||
console.log('get index') | ||
return send(Object.keys(store.store)) | ||
} else if(req.method == 'GET') { | ||
var obj = store.get (url) | ||
send(obj || {error: 'not_found', url: url}, obj ? 200 : 404) | ||
} else { | ||
//post: buffer body, and parse. | ||
var body = '', obj | ||
req.on('data', function (c) { body += c}) | ||
req.on('end', function (c) { | ||
try { | ||
obj = JSON.parse(body) | ||
} catch (err) { | ||
return send (err, 400) | ||
} | ||
store.set(url, obj) | ||
send({ok: true}) | ||
}) | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "http-proxy-examples" | ||
, "description": "packages required to run the examples" | ||
, "version": "0.0.0" | ||
, "dependencies": { | ||
"connect": "1.6" | ||
, "connect-gzip": "0.1" | ||
, "connect-jsonp": "0.0.5" | ||
, "connect-restreamer": "1" | ||
, "proxy-by-url": "0.0.0" | ||
} | ||
} |
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
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