I have a frontend (html) and Node.js backend (express). I want to make a request to a 3rd party api but having cors issues.
Now I want to be able to grab the value of an input and append it to my backend url upon request on frontend (e.g localbackend:3000/${input_value}), then use the passed parameter (input_value) in my backend to form the api url that I need to make a call to the external api.
A bit of code:
index.html
<input type="text" id="text">
index.js
document.getElementById("send").addEventListener("click", async (e) => {
e.preventDefault();
var message = document.getElementById("text").value;
if (message == undefined || message == "") {
} else {
let response = "";
await fetch(`http://localhost:3000/${message}`)
.then((data) => data.json())
.then((data) => response = data)
.catch((error) => console.error(error));
.... then response to be appended to another div...
server.js
app.get("/", (req, res) => {
axios
.request(
`http://someexternalserver/get?uid="simon"&msg=${where_i_need_the_input}`
)
.then(function (response) {
res.send(JSON.stringify(response.data));
})
.catch(function (error) {
console.error(error);
});
});
Manually I am able to successfully test the external api but I want this done dynamically.
Feel free to share a better solution or simply how to get this working. No FE frameworks please.
I tried:
Rob's cors-anywhere (https://cors-anywhere.herokuapp.com/) proxy which the api has restricted.