-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo-server.js
50 lines (39 loc) · 1.13 KB
/
demo-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env node
var http = require('http');
var sanitizer = require('./index.js');
var s = function(obj) { return JSON.stringify(obj, null, ' ') }
http.createServer(function (request, response) {
var write = function(data) {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(data);
response.end();
}
if(request.url.substr(1) == '') write("{'error':'no url'}");
else {
var extReq = require('request')
var url = request.url.substr(1)
var query = false
var firstQ = url.indexOf('?')
if (firstQ != -1) {
var nextQ = url.indexOf('?', firstQ+1)
if (nextQ != firstQ) {
query = url.substr(nextQ+1)
url = url.substr(0, nextQ)
}
else query = url.substr(firstQ+1)
query = unescape(query)
}
extReq.get(url, function(err, resp, body) {
if (err)
write(s({'error': err }))
else if (resp.statusCode != 200)
write(s({'response': response }))
else {
var qo = sanitizer.load(body)
var writeJson = function(obj) { write(s(obj)) }
if (query) qo = qo.query(query)
qo.obj(writeJson)
}
});
}
}).listen(7770);