add error message page

This commit is contained in:
hippoz 2020-10-02 23:12:51 +03:00
parent 1c1ab074ce
commit 006446b7b0
2 changed files with 17 additions and 4 deletions

View file

@ -77,11 +77,11 @@ app.post('/api/upload', (req, res) => {
const password = req.body.password;
if (config.passwords.indexOf(password) === -1) {
return res.status(401).send('Incorrect password.');
return res.status(401).render('uploadfailed', { message: 'The password you entered is not correct.' });
}
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
return res.status(400).render('uploadfailed', { message: 'You forgot to upload a file.' });
}
const file = req.files.file;
@ -89,14 +89,14 @@ app.post('/api/upload', (req, res) => {
fs.stat(filepath, (err) => {
if(err == null) {
return res.status(400).send('File exists.');
return res.status(400).render('uploadfailed', { message: 'A file with that name already 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.sendStatus(500);
return res.status(500).render('uploadfailed', { message: 'Something went wrong.' });;
}
});
});

13
views/uploaderror.ejs Normal file
View file

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
</head>
<body>
<h1>upload failed</h1>
<p>i think</p>
<p><%= message %></p>
</body>
</html>