const uploadFunction = event => { const fileInput = event.target; const files = event.target.files const path = event.target.value; const allowedExtensions = /(\.jpg|\.jpeg|\.png|\.gif)$/i; if (!allowedExtensions.exec(path)) { alert('Invalid file type'); fileInput.value = ''; return false; } const data = new FormData() data.append('file', files[0]) fetch('/upload-file', { method: 'POST', body: data }) .then(response => response.json()) .then(data => { console.log(data) }) .catch(error => { console.error(error) }) }