Added error middleware

This commit is contained in:
Wyatt Johnson
2016-11-08 15:53:26 -07:00
parent 571778f42b
commit a1fd4d32ba
4 changed files with 78 additions and 5 deletions
+1
View File
@@ -55,6 +55,7 @@
"no-var": [2],
"no-lonely-if": [2],
"curly": [2],
"no-unused-vars": ["error", { "argsIgnorePattern": "next" }],
"no-multiple-empty-lines": [
"error",
{"max": 1}
+48 -5
View File
@@ -11,14 +11,57 @@ app.use(bodyParser.json());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// API Routes.
// Routes.
app.use('/api/v1', require('./routes/api'));
app.use('/client', express.static(path.join(__dirname, 'dist')));
app.use('/admin', require('./routes/admin'));
// Static Routes.
app.use('/client/', express.static(path.join(__dirname, 'dist')));
//==============================================================================
// ERROR HANDLING
//==============================================================================
app.get('/admin*', (req, res) => {
res.render('admin', {basePath: '/client/coral-admin'});
const ErrNotFound = new Error('Not Found');
ErrNotFound.status = 404;
// Catch 404 and forward to error handler.
app.use((req, res, next) => {
next(ErrNotFound);
});
// General error handler. Respond with the message and error if we have it while
// returning a status code that makes sense.
if (app.get('env') === 'development') {
app.use('/api', (err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
app.use('/', (err, req, res, next) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
app.use('/api', (err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
});
});
app.use('/', (err, req, res, next) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
+9
View File
@@ -0,0 +1,9 @@
const express = require('express');
const router = express.Router();
router.get('*', (req, res) => {
res.render('admin', {basePath: '/client/coral-admin'});
});
module.exports = router;
+20
View File
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Error</title>
</head>
<body>
<div class="container">
<pre><%= message %></pre>
<pre><%= error.stack %></pre>
</div>
</body>
</html>