Sha256: 9bc1ff738ee5221857b398b25a7820c1f356987ef9422903fb3fd7cf5d5815ae

Contents?: true

Size: 1.63 KB

Versions: 17

Compression:

Stored size: 1.63 KB

Contents

//
// 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.
//
var Store = module.exports = 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})
        })
      } 
    }
  }
}

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
hooch-0.4.2 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
hooch-0.4.1 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
hooch-0.4.0 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
hooch-0.3.0 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
hooch-0.2.1 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
hooch-0.2.0 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
hooch-0.1.0 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
hooch-0.0.8 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
hooch-0.0.7 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
hooch-0.0.6 jasmine/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
entangled-0.0.16 spec/dummy/public/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
entangled-0.0.15 spec/dummy/public/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
entangled-0.0.14 spec/dummy/public/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
entangled-0.0.13 spec/dummy/public/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
entangled-0.0.12 spec/dummy/public/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
entangled-0.0.11 spec/dummy/public/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js
entangled-0.0.10 spec/dummy/public/node_modules/karma/node_modules/http-proxy/examples/helpers/store.js