When clicking the button on my front end (relevant portion shown below)
async function getSample() {
const res = await fetch('/lookup/url');
const data = await res.text();
console.log(data);
}
document.getElementById('button').addEventListener('click', getSample);
async function getSample() {
fetch('http://localhost:3000/lookup/url')
.then(response => response.text())
.then(data => console.log(data));
}
I am getting this error in the terminal of my node server:
TypeError [ERR_INVALID_URL]: Invalid URL: url
and this in my console:
Fetch failed loading: GET "http://localhost:3000/lookup/url"
Can anyone provide some advice as to what I may be doing wrong and how I could fix?
Not that this matters, but backend is express
Adjustment Re: Comments
async function getSample() {
const res = await fetch('/lookup/url');
const data = await res.text();
console.log(data);
document.getElementById('button').addEventListener
('click', getSample);
fetch('http://localhost:3000/lookup/url')
.then(response => response.text())
.then(data => console.log(data));
}
Get rid of the second definition of getSample(). You only need one definition of the function, and it's better to use a relative URL to access resources on the same server as the front end.
async function getSample() {
const res = await fetch('/lookup/url');
const data = await res.text();
console.log(data);
}
document.getElementById('button').addEventListener('click', getSample);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With