Not sure why my route is returning a 404 error

Viewed 68

So I'm pretty new to node and express stuff but I'm trying to learn. I built a file uploader and I want to pass that file to my bucket in google cloud. However every time I try and send the data I end up getting a 404 error and I'm not totally sure why. I know my endpoint is being hit because I'm getting the 404 cant find upload on port so I think the issue may be I set up my route handling "/" incorrectly. Any insight would be really helpful thank you.

Frontend code:

const fileUpload = () =>{

if(files.length > 0){
  for(let i = 0; i < files.length; i++){
    fetch('/upload',{
      method: "Post",
      body:files[i]
    })
      .then(res => res.text())
      .then((x) => console.log(x))
  }
}else{
  window.alert("No files have been selected to be uploaded")
}

}

Backend code:

app.get('/',(req,res)=>{
  res.sendFIle(__dirname + 'Upload.js')
})

let projectId = '###'
let keyFilename = '###'

const storage = new Storage({
 projectId,
 keyFilename
})

const bucket  = storage.bucket('###')

app.post('/upload',(req,res) =>{
console.log('made it to upload')
try{
    if(req.file){
        console.log('file found trying to upload....')
        const blob = bucket.file(req.file.path)
        const blobstream = blob.createWriteStream();

        blobstream.on('finish',() => {
            res.status(200).send('Success')
        })
        blobstream.end(req.file.bugger);
    }
}catch(error){
    res.status(500).send(error);
}

})

Edit/Solution: The issue was I was not entering the entire route for my fetch on where I called the endpoint. So the front end that says fetch('upload') should of been fetch('http://localhost:5000/upload')

0 Answers
Related