-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphmock.js
More file actions
72 lines (57 loc) · 1.63 KB
/
graphmock.js
File metadata and controls
72 lines (57 loc) · 1.63 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var http = require('http');
var https = require('https');
var qs = require('querystring');
var request = require('request');
var url = require('url');
function server (req, res) {
var store = function (err, data) {
if (err) return respond(err);
storeData(req.url, data, query);
};
var query = function (err, data) {
if (err) return respond(err);
queryData(req.url, respond);
};
var respond = function (err, data) {
var status = err ? 500 : 200;
res.writeHead(status, {'Content-Type': 'application/json'});
res.end(JSON.stringify(err || data));
};
// Read in the request body
req.setEncoding('utf8');
req.on('data', function (chunk) { req.body = (req.body || '') + chunk; });
req.on('end', function () {
if (process.argv.toString().indexOf('--passthrough') > -1) {
queryFacebook(req, store);
}
else {
query();
}
});
}
function queryFacebook (req, callback) {
request({
uri: 'https://' + 'graph.facebook.com' + req.url,
method: req.method,
body: req.body
}, function (err, response, body) {
try {
body = JSON.parse(body);
}
catch (e) { err = e; }
return callback(err, body);
});
}
var store = {};
function storeData (path, data, callback) {
console.log('Pat saves data to the store here!');
store[path] = data;
return callback(null, data);
}
function queryData (path, callback) {
console.log('Pat queries the store here!');
return callback(null, store[path]);
}
var port = process.argv[2] || 8080;
http.createServer(server).listen(port, '127.0.0.1');
console.log('GraphMock running at http://127.0.0.1:' + port + '/');