37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
const http = require('http')
|
|
|
|
module.exports = class http_client {
|
|
constructor(ip, port) {
|
|
this.#ip = ip
|
|
this.#port = port
|
|
}
|
|
async post(data, callback_answer, callback_error) {
|
|
// Build the post string from an object
|
|
|
|
// An object of options to indicate where to post to
|
|
var post_options = {
|
|
host: this.#ip,
|
|
port: this.#port,
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'applicaiton/json',
|
|
'Content-Length': Buffer.byteLength(data)
|
|
}
|
|
}
|
|
// Set up the request
|
|
var request = http.request(post_options, function(res) {
|
|
res.setEncoding('utf8')
|
|
res.on('data', async function (answer) {
|
|
callback_answer(answer)
|
|
})
|
|
})
|
|
request.on('error', function(e) {
|
|
callback_error(e)
|
|
})
|
|
// post the data
|
|
request.write(data)
|
|
request.end()
|
|
}
|
|
#ip
|
|
#port
|
|
} |