Search code examples
node.jsazuremulter

Empty req.body when using MulterAzureStorage


We have an application in which we use a form to send information and files to our blob storage. We are using the MulterAzureStorage package for this. At the same time we are using middleware such that the user can authenticate themself. The authentication middleware has been used before and it is known that it works correctly. When we are following the workflow without the authentication and multer middleware, then the req.body param is filled with the data of the form including the files. Unfortunately, when we add both of the middleware, then the req.body is empty and no file is stored in the container of the blob storage.

The structure of the code looks like this:

const azureStorage = new MulterAzureStorage({
  connectionString: AZURE_CONNECTION_STRING,
  accessKey: AZURE_ACCESS_KEY,
  accountName: AZURE_STORAGE_ACCOUNT_NAME,
  containerName: CONTAINER_NAME,
  blobName: BLOB_NAME,
});

const upload = multer({
  storage: azureStorage
});

router.post('/verzoek',[auth,upload.any()], async (req, result) => {
   console.log(req.body)
   // Our code continues below but it is removed for simplicity
}

Since we can read the req.body without the middleware, we think that the request is alright. However, just to be sure, the post request goes as follows:

const request = new FormData();

// At this point we filled the variable request but we removed it for simplicity

$.ajax({
          async: true,
          method: 'POST',
          url: '/aanvragen/verzoek',
          data: request,
          cache: false,
          processData: false,
          contentType: false,
            
          xhr: function () {
              var xhr = new XMLHttpRequest();
              return xhr;
          }
      }).done(function (result) {
          if (result == "Succes") {
              // Activate some classes
          } else {
              // An alert
          }
      }).fail(function (xhr, status) {
          // An error message
      });

To conclude, there is something going wrong in the middleware such that the body of the req has no content. We tried some suggestions but haven't had any success. Hope somebody can help us with finding the problem! Thank you in advance!


Solution

    • I created storage account in azure portal and create a container.

    enter image description here

    • Check the below code that works for me.
    const express = require('express');
    const multer = require('multer');
    const { BlobServiceClient } = require('@azure/storage-blob');
    const app = express();
    const authMiddleware = (req, res, next) => {
      next(); 
    };
    const multerMiddleware = multer().single('file');
    const connectionString ='#######';  
    const containerName = 'pavan123';
    app.post('/upload', authMiddleware, multerMiddleware, async (req, res) => {
      const file = req.file;
      const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
      const containerClient = blobServiceClient.getContainerClient(containerName);
      const blobName = file.originalname;
      const blockBlobClient = containerClient.getBlockBlobClient(blobName);
      const uploadResponse = await blockBlobClient.uploadData(file.buffer);
      res.send('File uploaded successfully.');
    });
    const port = 3000;  
    app.listen(port, () => {
      console.log(`Server started on port ${port}`);
    });
    
    
    • When I run the above code I got as given below. enter image description here

    • The command need to use npm install express multer @azure/storage-blob.

    • I use the postman to POST form data file to container. enter image description here

    • I just checked whether the file is uploaded in my cloud storage account. enter image description here