diff --git a/index.js b/index.js index 61b78f2..9f5a67d 100644 --- a/index.js +++ b/index.js @@ -87,14 +87,16 @@ app.post('/api/upload', (req, res) => { const file = req.files.file; const filepath = `${config.storagePath}/${file.name}`; - fs.exists(filepath, (exists) => { - if (!exists) { + fs.stat(filepath, (err) => { + if(err == null) { + return res.status(400).send('File exists.'); + } else if(err.code === 'ENOENT') { file.mv(filepath, (err) => { if (err) return res.status(500).send(err); res.render('uploaded', { file: { name: file.name } }); }); } else { - return res.status(400).send('File exists.'); + return res.sendStatus(500); } }); });