From 36ddeab64e3208ddf8084752b880265861b5bc8b Mon Sep 17 00:00:00 2001 From: lesion Date: Sun, 26 Aug 2018 23:41:51 +0200 Subject: [PATCH] refactoring env/dev/prod/404/500 --- app.js | 46 +++++++++++++++++++++++++--------------------- package.json | 1 - 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/app.js b/app.js index f5d4392..5948978 100644 --- a/app.js +++ b/app.js @@ -1,34 +1,38 @@ -var createError = require('http-errors'); -var express = require('express'); -var path = require('path'); +const express = require('express') +const path = require('path') +const indexRouter = require('./routes/index') +const { MAINTENANCE, BASE, DEV } = require('./config') -var indexRouter = require('./routes/index'); - -var app = express(); +const app = express() // view engine setup -app.set('views', path.join(__dirname, 'views')); -app.set('view engine', 'pug'); +app.set('views', path.join(__dirname, 'views')) +app.set('view engine', 'pug') -app.use(express.urlencoded({ extended: false })); -app.use(express.static(path.join(__dirname, 'public'))); +app.use(express.urlencoded({ extended: false })) +app.use(express.static(path.join(__dirname, 'public'))) -app.use('/', indexRouter); +app.locals.base = BASE -// catch 404 and forward to error handler -app.use(function(req, res, next) { - next(createError(404)); -}); +if (MAINTENANCE) { + app.get('/', (req, res) => res.render('maintenance')) + app.use('/test', indexRouter) +} else { + app.use('/', indexRouter) +} + +// catch 404 +app.use( (req, res) => res.render('404') ) // error handler app.use(function(err, req, res, next) { // set locals, only providing error in development - res.locals.message = err.message; - res.locals.error = req.app.get('env') === 'development' ? err : {}; + res.locals.message = DEV ? err.message : '' + res.locals.error = DEV ? err : {} // render the error page - res.status(err.status || 500); - res.render('error'); -}); + res.status(err.status || 500) + res.render('error') +}) -module.exports = app; +module.exports = app diff --git a/package.json b/package.json index 286fb79..012f1cf 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ }, "dependencies": { "express": "^4.16.3", - "http-errors": "^1.7.0", "moment": "^2.22.2", "pug": "2.0.0-beta11", "request": "^2.88.0",