Search code examples
node.jsreactjspathxmlhttprequest

Cannot find the file URL using xhr.open('post',url,true) - Status: 404 node js


Trying to Upload an image by XMLHttpRequest(), have issue understanding what is the correct URL to access file via xhr.open(...).

Following this example for server side code and everything..

var xhr = new XMLHttpRequest();

  xhr.open('post','../../../../server/routes/saveImage.js',true);
  xhr.onload = function() {
    if(this.status ==200) {
      resolve(this.response);
    } else {
      reject (this.statusText);
    }
  };

The Project directory is something like this

  • Project
    • client
      • app
        • component
          • product
            • addproduct.js <-- xhr.open is called from here
    • server
      • routes
        • saveImage.js <-- File being called

Also regarding paths let me know if there is a more convenient way to check the access path or absolute path to use in url.


Solution

  • The problem indeed was with setting up a server side route, this was mostly clarified, thanks to the answer from @Arkita and comment from @Kasper. But I went ahead to dig for a solution, which may not be very useful to others as this was a dumb question in the first place, but here it goes..

    On client side

    xhr.open('post','/saveImage/save',true);
    

    on server side if you are using Express.js or other connect based frameworks

    app.post('/saveImage/save',(req,res,next)=> {....})
    

    Also the example I linked above may be outdated, this seems more helpful.