Search code examples
node.jsfilemammoth

cannot access the files in my express route


I'm trying to do the following:

upload a doc file to the a specific express route, and then use mammoth to parse it to an html object,

this is the library: https://www.npmjs.com/package/mammoth

My problem is that it takes the file in the documentation straight from the route, which is not what I want, I want to upload a file to my route and parse it...

router.post("/parseCV", (req, res) => {
  console.log("entered route");
  console.log(req.files);
});

Im uploading the file like that :

 const onSubmit = e => {
    e.preventDefault();
    const formData = new FormData();
    formData.append("file", file);
    uploadImage(formData);
  };

how can I access it in my route ? req.files,req.body are all undefined


Solution

  • Take a look at multer module which let you deal with multipart request.

    Example:

    const multer = require('multer');
    const upload = multer({ dest: '/temp/uploads/' }); // Destination of where you wanna upload your file
    
    router.post("/parseCV", upload.single('file'), (req, res) => {
        console.log("entered route");
        console.log(req.file); // Contains the path of your file.
        // DO WHATEVER YOU WANT TO DO WITH YOUR FILE
    });