-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpjson.js
More file actions
55 lines (52 loc) · 1.38 KB
/
Copy pathhttpjson.js
File metadata and controls
55 lines (52 loc) · 1.38 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
var http = require('http');
exports.get = function get(domain, port, path, readData, onSuccess, onFail) {
var req = http.request({
'host' : domain,
'port' : port,
'path' : path,
'headers' : {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(readData)
}
});
req.on('response', function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
onSuccess(body);
});
});
req.on('error', function(response) {
onFail();
});
req.write(readData);
req.end();
}
exports.post = function post(domain, port, path, writeData, onSuccess, onFail) {
var req = http.request({
'host' : domain,
'port' : port,
'path' : path,
'method' : 'POST',
'headers' : {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(writeData)
}
});
req.on('response', function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
onSuccess(body);
});
});
req.on('error', function(response){
onFail();
});
req.write(writeData);
req.end();
}