Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / examples / middleware / bodyDecoder-middleware.js
1
2 var Store = require('../helpers/store')
3   , http = require('http')
4
5 http.createServer(new Store().handler()).listen(7531, function () {
6 //try these commands:
7 // get index:
8 // curl localhost:7531
9 // []
10 //
11 // get a doc:
12 // curl localhost:7531/foo
13 // {"error":"not_found"}
14 //
15 // post an doc:
16 // curl -X POST localhost:7531/foo -d '{"content": "hello", "type": "greeting"}'
17 // {"ok":true}
18 //
19 // get index (now, not empty)
20 // curl localhost:7531
21 // ["/foo"]
22 //
23 // get doc 
24 // curl localhost:7531/foo
25 // {"content": "hello", "type": "greeting"}
26
27 //
28 // now, suppose we wanted to direct all objects where type == "greeting" to a different store 
29 // than where type == "insult"
30 //
31 // we can use connect connect-bodyDecoder and some custom logic to send insults to another Store.
32
33 //insult server:
34
35   http.createServer(new Store().handler()).listen(2600, function () {
36
37   //greetings -> 7531, insults-> 2600 
38
39   // now, start a proxy server.
40
41     var bodyParser = require('connect/lib/middleware/bodyParser')
42     //don't worry about incoming contont type
43     //bodyParser.parse[''] = JSON.parse
44
45     require('../../lib/node-http-proxy').createServer(
46       //refactor the body parser and re-streamer into a separate package
47       bodyParser(),
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})
58       }
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) {
64         request.post({
65           url: 'http://localhost:1337/' + greeting,
66           json: {content: greeting, type: type || "greeting"}
67         })
68       }
69       post("hello")
70       post("g'day")
71       post("kiora")
72       post("houdy")
73       post("java", "insult")
74
75       //now, the insult should have been proxied to 2600
76       
77       //curl localhost:2600
78       //["/java"]
79
80       //but the greetings will be sent to 7531
81
82       //curl localhost:7531
83       //["/hello","/g%27day","/kiora","/houdy"]
84
85     })
86   })
87 })