JSFiddle - React, Tailwind, and code Playground
by brigand
JavaScript
app.get("/getPenals", function(req, res) {
let url = "https://a..................73777c6";
get(url).then((body) => {
res.json(body);
})
.catch(error => {
// send error response
})
});
const headers = {
"Authorization": "Basic ZGRh.........KUUs=",
"Accept": "application/json"
};
let api = (url, method, body = null) => {
let params = {
headers,
body,
"method": method
}
fetch(url, params)
.then(res => {
const contentType = res.headers.get("content-type") || '';
const bodyPromise = contentType.includes("application/json") ?
res.json() :
res.text()
return bodyPromise.then((body) => {
if (res.ok) {
return body;
} else {
// create and throw error including res.statusCode and body
}
})
})
};
let get = (url) => {
return api(url, "GET");
};
let post = (url, body) => {
return api(url, "POST", body);
};