There are two ways that I try to figure out if a URL exists.
Option 1
function fileExists(url, callback) {
fetch(url, { method: 'head' })
.then((status) => {
callback(status.ok);
});
}
Option 2
function fileExists(url) {
const http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status !== 404;
}
Both of them work but I still get the error:
HEAD http://localhost:4200/api/uploads/profile-images/a68e2578adb131bd8c6abfde2c729055.png 404 (Not Found)
I don't want to show this error after I have figured out that the URL does not exist. How can I prevent that error?