add error message page
This commit is contained in:
parent
1c1ab074ce
commit
006446b7b0
2 changed files with 17 additions and 4 deletions
8
index.js
8
index.js
|
@ -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
13
views/uploaderror.ejs
Normal 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>
|
Loading…
Reference in a new issue