express file upload mv

Solutions on MaxInterview for express file upload mv by the best coders in the world

showing results for - "express file upload mv"
Jona
16 Jan 2020
1app.post('/upload-avatar', async (req, res) => {
2    try {
3        if(!req.files) {
4            res.send({
5                status: false,
6                message: 'No file uploaded'
7            });
8        } else {
9            //Use the name of the input field (i.e. "avatar") to retrieve the uploaded file
10            let avatar = req.files.avatar;
11            
12            //Use the mv() method to place the file in upload directory (i.e. "uploads")
13            avatar.mv('./uploads/' + avatar.name);
14
15            //send response
16            res.send({
17                status: true,
18                message: 'File is uploaded',
19                data: {
20                    name: avatar.name,
21                    mimetype: avatar.mimetype,
22                    size: avatar.size
23                }
24            });
25        }
26    } catch (err) {
27        res.status(500).send(err);
28    }
29});
30