Created
September 23, 2020 00:42
-
-
Save redeemefy/56b35e760d49ebed2cb55ec8bd141048 to your computer and use it in GitHub Desktop.
ElasticSearch REST client that allows body in GET requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require("https") | |
const http = require("http") | |
const fetch = require("node-fetch") | |
const ElasticSearchRestClient = { | |
get: function(body, options = {}) { | |
return new Promise((resolve, reject) => { | |
const callback = function(response) { | |
let str = "" | |
response.on("data", function(chunk) { | |
str += chunk | |
}) | |
response.on("end", function() { | |
resolve(JSON.parse(str)) | |
}) | |
} | |
const req = (options.protocol === "https:" ? https : http).request(options, callback) | |
req.on("error", err => { | |
reject(err) | |
}) | |
if(body !== null) { | |
req.write(body) | |
} | |
req.end() | |
}) | |
}, | |
post: async function(url, options, body) { | |
// const url = `${options.protocol}//${options.host}:${options.port}${options.path}` | |
const response = await fetch(url, { method: "POST", headers: options.headers, body: body }) | |
return await response.json() | |
} | |
} | |
module.exports = { ElasticSearchRestClient } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment