-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.ts
83 lines (71 loc) · 2.23 KB
/
demo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Demo Server
*/
import bodyParser from 'body-parser';
import d from 'debug';
import express, { NextFunction, Request, Response } from 'express';
import logger from 'morgan';
const debug = d('express-errorhandlers:demo');
import { Handler } from './src';
import { errorHandler, notFound, skipOkHandler } from './src/middleware';
///
const PORT = 3000;
const app = express();
app.use(logger('combined'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.get('/', (_req: Request, res: Response, _next: NextFunction) => {
res.status(200).json({
'Access URLs': [
'http://localhost:3000/401',
'http://localhost:3000/502',
'http://localhost:3000/500'
]
});
});
app.get('/401', (_req: Request, _res: Response, next: NextFunction) => {
next(new Handler(undefined, 401, 'Unauthorized', {
code: 'A-401-000000'
}));
});
app.get('/502', (_req: Request, _res: Response, next: NextFunction) => {
next(new Handler(undefined, 502, 'Bad Gateway', {
code: 'A-502-000000'
}));
});
app.get('/500', (req: Request, _res: Response, next: NextFunction) => {
next(new Error(`${req.path} Server Error!!`));
});
///
app.use(skipOkHandler(
['/favicon.ico', '/sitemap.xml'],
// fn: function() {...}
));
app.use(notFound(
'Not Found :p', {
message: 'page not found.'
}, {
env: process.env.NODE_ENV
},
));
app.use(errorHandler({
debug: process.env.NODE_ENV !== 'production',
extra: { message: 'page server error.' }, // Extended message object
extraDebug: { env: process.env.NODE_ENV }, // Extended message object (only debug)
final: (_req, _res, handler) => {
// console.error('final. error:', handler); // log output
debug('final call. %O', handler);
},
// templateHTML: {...}, // pug template string or pug file path (HTML)
// templateHTMLOptions: {...}, // pug compile config (HTML)
// templateTEXT: {...}, // pug template string or pug file path (TEXT)
// templateTEXTOptions: {...}, // pug compile config (TEXT)
message: 'Demo Server Error', // default error message
status: 555, // default response status code
}));
if (process.env.EXPRESS_ERRROHANDLERS_LISTEN) {
app.listen(PORT, () => console.log(`demo app listening on port ${PORT}!`));
}
export default app;