2 var Store = require('../helpers/store')
3 , http = require('http')
5 http.createServer(new Store().handler()).listen(7531, function () {
12 // curl localhost:7531/foo
13 // {"error":"not_found"}
16 // curl -X POST localhost:7531/foo -d '{"content": "hello", "type": "greeting"}'
19 // get index (now, not empty)
20 // curl localhost:7531
24 // curl localhost:7531/foo
25 // {"content": "hello", "type": "greeting"}
28 // now, suppose we wanted to direct all objects where type == "greeting" to a different store
29 // than where type == "insult"
31 // we can use connect connect-bodyDecoder and some custom logic to send insults to another Store.
35 http.createServer(new Store().handler()).listen(2600, function () {
37 //greetings -> 7531, insults-> 2600
39 // now, start a proxy server.
41 var bodyParser = require('connect/lib/middleware/bodyParser')
42 //don't worry about incoming contont type
43 //bodyParser.parse[''] = JSON.parse
45 require('../../lib/node-http-proxy').createServer(
46 //refactor the body parser and re-streamer into a separate package
48 //body parser absorbs the data and end events before passing control to the next
49 // middleware. if we want to proxy it, we'll need to re-emit these events after
50 //passing control to the middleware.
51 require('connect-restreamer')(),
52 function (req, res, proxy) {
53 //if your posting an obect which contains type: "insult"
54 //it will get redirected to port 2600.
55 //normal get requests will go to 7531 nad will not return insults.
56 var port = (req.body && req.body.type === 'insult' ? 2600 : 7531)
57 proxy.proxyRequest(req, res, {host: 'localhost', port: port})
59 ).listen(1337, function () {
60 var request = require('request')
61 //bodyParser needs content-type set to application/json
62 //if we use request, it will set automatically if we use the 'json:' field.
63 function post (greeting, type) {
65 url: 'http://localhost:1337/' + greeting,
66 json: {content: greeting, type: type || "greeting"}
73 post("java", "insult")
75 //now, the insult should have been proxied to 2600
80 //but the greetings will be sent to 7531
83 //["/hello","/g%27day","/kiora","/houdy"]