refactoring env/dev/prod/404/500
This commit is contained in:
parent
2aa6383cc4
commit
36ddeab64e
2 changed files with 25 additions and 22 deletions
46
app.js
46
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
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue