From 6fb8ecd44738bf5bf19a01953caa30635d38e952 Mon Sep 17 00:00:00 2001 From: Rosanny <rosanny.sihombing@hft-stuttgart.de> Date: Thu, 19 Mar 2020 13:39:58 +0100 Subject: [PATCH] handle 404 and 500 pages --- app.js | 16 ++++++++++++++-- routes/routes-account.js | 14 ++++++++------ views/DE/404.pug | 34 ++++++++++++++++++++++++++++++---- views/DE/500.pug | 39 +++++++++++++++++++++++++++++++-------- views/DE/error.pug | 6 ------ 5 files changed, 83 insertions(+), 26 deletions(-) delete mode 100644 views/DE/error.pug diff --git a/app.js b/app.js index debd5c3..f3ed756 100644 --- a/app.js +++ b/app.js @@ -58,12 +58,24 @@ app.use(function(req, res, next) { next(); }); -//require('./routes/routes')(app, config, passport, i18n); require('./routes/routes-account')(app, config, passport, i18n); require('./routes/routes-project')(app, config, passport); -//require('./routes/dbconn')(app, config); require('./routes/api')(app, config, passport); +// Handle 404 +app.use(function (req, res, next) { + //res.status(404).send('404: Page not Found', 404) + res.status(404).render('./DE/404') +}) + +// Handle 500 - any server error +app.use(function (err, req, res, next) { + console.error(err.stack) + res.status(500).render('./DE/500', { + error: err + }) +}) + app.listen(app.get('port'), function () { console.log('Express server listening on port ' + app.get('port')); }); \ No newline at end of file diff --git a/routes/routes-account.js b/routes/routes-account.js index 84ddbec..b3bbc5e 100644 --- a/routes/routes-account.js +++ b/routes/routes-account.js @@ -108,10 +108,6 @@ module.exports = function (app, config, passport, i18n) { } }); - app.get('/error', function (req, res) { - res.render(lang+'/error') - }); - app.get('/login', passport.authenticate(config.passport.strategy, { @@ -272,7 +268,10 @@ module.exports = function (app, config, passport, i18n) { // Load hashed passwd from DB dbconn.user.query('SELECT password FROM credential WHERE user_id='+userId, function (err, rows, fields) { if (err) { - res.redirect('/500') + //res.redirect('/500') + res.status(500).render(lang+'/500', { + error: err + }) throw err } var userPwd = rows[0].password @@ -280,7 +279,10 @@ module.exports = function (app, config, passport, i18n) { // check if the password is correct bcrypt.compare(currPwd, userPwd, function(err, isMatch) { if (err) { - res.redirect('/500') + //res.redirect('/500') + res.status(500).render(lang+'/500', { + error: err + }) throw err } else if (!isMatch) { diff --git a/views/DE/404.pug b/views/DE/404.pug index dc9e0a8..a0a6044 100644 --- a/views/DE/404.pug +++ b/views/DE/404.pug @@ -1,4 +1,30 @@ -extends error - -block content - h2 Cannot find #{url} \ No newline at end of file +doctype html +html(lang="de") + head + title= "404 - Page not found" + meta(charset="UTF-8") + meta(name="viewport", content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no") + link(rel="stylesheet", type="text/css", href="https://transfer.hft-stuttgart.de/css/bootstrap/bootstrap.css") + style. + .container { + height: 400px; + position: relative; + } + .center { + margin: 0; + position: absolute; + top: 50%; + left: 50%; + -ms-transform: translate(-50%, -50%); + transform: translate(-50%, -50%); + } + body + div(class="container") + div(class="center", align="center") + a(href="https://m4lab.hft-stuttgart.de") + img(src="https://transfer.hft-stuttgart.de/images/demo/m4lab_logo.jpg", class="img-responsive center-block", width="185", height="192") + br + br + p 404. The requested URL was not found. + // Bootstrap + script(src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous") \ No newline at end of file diff --git a/views/DE/500.pug b/views/DE/500.pug index cbc47e7..9614b9e 100644 --- a/views/DE/500.pug +++ b/views/DE/500.pug @@ -1,8 +1,31 @@ -extends error - -block content - h1 Error: #{error.message} - if settings['verbose errors'] - pre= error.stack - else - p An error ocurred! \ No newline at end of file +doctype html +html(lang="de") + head + title= "500 - Internal Server Error" + meta(charset="UTF-8") + meta(name="viewport", content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no") + link(rel="stylesheet", type="text/css", href="https://transfer.hft-stuttgart.de/css/bootstrap/bootstrap.css") + style. + .container { + height: 400px; + position: relative; + } + .center { + margin: 0; + position: absolute; + top: 50%; + left: 50%; + -ms-transform: translate(-50%, -50%); + transform: translate(-50%, -50%); + } + body + div(class="container") + div(class="center", align="center") + a(href="https://m4lab.hft-stuttgart.de") + img(src="https://transfer.hft-stuttgart.de/images/demo/m4lab_logo.jpg", class="img-responsive center-block", width="185", height="192") + br + br + p 500. Unexpected Error :( + p #{ error } + // Bootstrap + script(src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous") \ No newline at end of file diff --git a/views/DE/error.pug b/views/DE/error.pug deleted file mode 100644 index bf750c0..0000000 --- a/views/DE/error.pug +++ /dev/null @@ -1,6 +0,0 @@ -html - head - title Error - body - h1 An error occurred! - block content \ No newline at end of file -- GitLab