Q: how to send just the filename in response of multer file upload hi I am using express and multer to receive multiple files in backend from react frontend my react query code is like this:
fileChangeHandler = (event) => {
        const data = new FormData()
        let files = event.target.files
        for (let file of files) {
            data.append('file', file)
        }
        let url = db.url + "/adminendpoint/uploadfile"
        axios.post(url, data, {
            headers: {
                'Content-Type': 'application/json',
                Authorization:  this.props.token
            },
        }).then(r => console.log(r.names))
    }

the backend data for multer is like this :

uploadFile = async (req,res,next)=>{
    let storage = multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, 'public/images')
        },
        filename: function (req, file, cb) {
            cb(null, Date.now() + '-' +file.originalname )
        }
    })

    let upload = multer({storage:storage}).array('file')
    upload(req, res, function (err) {
        if (err instanceof multer.MulterError) {
            return res.json({err:err})
        } else if (err) {
            return res.json({err:err})
        }
        let names=[]
        req.files.map(f=> names.push(f.filename))
        console.log(names);
        res.setHeader('Content-Type', 'application/json');
        return res.json({names:names})
    })



but the weird thing is that in the backend the names is an array holding the names of uploaded files but in the frontend the response is an object with a lot of data like this. it has the names array in it but I want to stop sending this chunk of data from my backend and only send the file names and the response format should only be json
config: {url: "http://localhost:8090/adminendpoint/uploadfile", method: "post", data: FormData, headers: {…}, transformRequest: Array(1), …}
data:
names: (2) ["1603483842517-2.png", "1603483842518-3.png"]
__proto__: Object
headers: {content-length: "54", content-type: "application/json; charset=utf-8"}
request: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …}
status: 200
statusText: "OK"
__proto__: Object

​


A: You're all set, just use the data property from the Axios response object.
console.log(r.data.names)

Axios returns a response object that contains more than just the response body. You'll get status, headers, config, request. The complete field list is availabel in Axios docs.
Also, a few side notes. You wouldn't want to set 'Content-Type': 'application/json' in React, since you're using FormData - it'll set 'Content-Type': 'multipart/form-data' behind the scenes. And in Express, if you're using res.json(), you don't have to set content type at all since that function already sets it.
