3 // just to make these example a little bit interesting,
4 // make a little key value store with an http interface
5 // (see couchbd for a grown-up version of this)
9 // retrive list of keys
12 // retrive object stored at [url]
13 // will respond with 404 if there is nothing stored at [url]
17 // JSON.parse the body and store it under [url]
18 // will respond 400 (bad request) if body is not valid json.
20 // TODO: cached map-reduce views and auto-magic sharding.
22 var Store = module.exports = function Store () {
28 return this.store[key]
30 set: function (key, value) {
31 return this.store[key] = value
35 return function (req, res) {
36 function send (obj, status) {
37 res.writeHead(200 || status,{'Content-Type': 'application/json'})
38 res.write(JSON.stringify(obj) + '\n')
41 var url = req.url.split('?').shift()
43 console.log('get index')
44 return send(Object.keys(store.store))
45 } else if (req.method == 'GET') {
46 var obj = store.get (url)
47 send(obj || {error: 'not_found', url: url}, obj ? 200 : 404)
49 //post: buffer body, and parse.
51 req.on('data', function (c) { body += c})
52 req.on('end', function (c) {
54 obj = JSON.parse(body)
56 return send (err, 400)