This repository has been archived by the owner on Feb 16, 2020. It is now read-only.
/
example.js
108 lines (97 loc) · 2.29 KB
/
example.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
;(function () {
'use strict'
var HTTPClient
// Node.js, browserify, ...
if (typeof module !== 'undefined' && module.exports) {
HTTPClient = require('..') // require('httpclient') for you
// browsers
} else {
HTTPClient = window.HTTPClient
}
var options = {
hostname: 'localhost',
path: '/',
port: 8080,
secure: false,
method: 'GET',
headers: {
'x-powered-by': 'HTTPClient.js'
},
body: 'hello'
}
console.log('options', options)
/*
* Example 1 - node style
*/
var example1 = new HTTPClient(options)
example1.request('/echo', function (err, res, body) {
console.log('Example 1')
console.log('error', err)
console.log('response', res)
console.log('body', body)
})
/*
* Example 2 - old school
*/
var example2 = new HTTPClient(options)
var req = example2.request('/echo')
console.log('Example 2')
req.onerror = function (err) {
console.log('error', err)
}
req.onresponse = function (res) {
console.log('response', res)
res.onend = function (body) {
console.log('body', body)
}
}
/*
* Example 3 - promise
*/
// TODO
/*
* Example 4 - test
*/
var http = HTTPClient
http(options, function (err, res, body) {
console.log('Example 4')
console.log('error', err)
console.log('response', res)
console.log('body', body)
})
// req.onerror = function(err) {
// console.log('Example 1')
// console.log(err)
// }
// req.onresponse = function(res) {
// console.log('Example 1')
// console.log('type: ' + res.type)
// console.log('status: ' + res.status)
// console.log('headers: ', res.headers)
// }
// req.onend = function(body) {
// console.log('Example 1')
// console.log('body: ')
// console.log(body.toString())
// }
/*
* Example 2
*/
// var httpclient = HTTPClient
// httpClient.
// options.body = {
// greetings: 'hello'
// }
// options.query = {
// foo: 'bar'
// }
// httpclient(options, function(err, res) {
// if (err)
// return console.log(err)
// console.log('Example 2')
// console.log('type: ', res.type)
// console.log('status: ', res.status)
// console.log('headers: ', res.headers)
// console.log('body: ', res.body.toString())
// })
})()