refactoring env/dev/prod/404/500
This commit is contained in:
parent
2aa6383cc4
commit
36ddeab64e
46
app.js
46
app.js
@ -1,34 +1,38 @@
|
|||||||
var createError = require('http-errors');
|
const express = require('express')
|
||||||
var express = require('express');
|
const path = require('path')
|
||||||
var path = require('path');
|
const indexRouter = require('./routes/index')
|
||||||
|
const { MAINTENANCE, BASE, DEV } = require('./config')
|
||||||
|
|
||||||
var indexRouter = require('./routes/index');
|
const app = express()
|
||||||
|
|
||||||
var app = express();
|
|
||||||
|
|
||||||
// view engine setup
|
// view engine setup
|
||||||
app.set('views', path.join(__dirname, 'views'));
|
app.set('views', path.join(__dirname, 'views'))
|
||||||
app.set('view engine', 'pug');
|
app.set('view engine', 'pug')
|
||||||
|
|
||||||
app.use(express.urlencoded({ extended: false }));
|
app.use(express.urlencoded({ extended: false }))
|
||||||
app.use(express.static(path.join(__dirname, 'public')));
|
app.use(express.static(path.join(__dirname, 'public')))
|
||||||
|
|
||||||
app.use('/', indexRouter);
|
app.locals.base = BASE
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
if (MAINTENANCE) {
|
||||||
app.use(function(req, res, next) {
|
app.get('/', (req, res) => res.render('maintenance'))
|
||||||
next(createError(404));
|
app.use('/test', indexRouter)
|
||||||
});
|
} else {
|
||||||
|
app.use('/', indexRouter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// catch 404
|
||||||
|
app.use( (req, res) => res.render('404') )
|
||||||
|
|
||||||
// error handler
|
// error handler
|
||||||
app.use(function(err, req, res, next) {
|
app.use(function(err, req, res, next) {
|
||||||
// set locals, only providing error in development
|
// set locals, only providing error in development
|
||||||
res.locals.message = err.message;
|
res.locals.message = DEV ? err.message : ''
|
||||||
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
res.locals.error = DEV ? err : {}
|
||||||
|
|
||||||
// render the error page
|
// render the error page
|
||||||
res.status(err.status || 500);
|
res.status(err.status || 500)
|
||||||
res.render('error');
|
res.render('error')
|
||||||
});
|
})
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.16.3",
|
"express": "^4.16.3",
|
||||||
"http-errors": "^1.7.0",
|
|
||||||
"moment": "^2.22.2",
|
"moment": "^2.22.2",
|
||||||
"pug": "2.0.0-beta11",
|
"pug": "2.0.0-beta11",
|
||||||
"request": "^2.88.0",
|
"request": "^2.88.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user