-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.example.ts
More file actions
36 lines (32 loc) · 1.1 KB
/
middleware.example.ts
File metadata and controls
36 lines (32 loc) · 1.1 KB
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
import express from 'express';
import {
enhanceReqWithTransactionAndTime,
GLogger,
LoggingMode,
responseErrorLoggerFactory,
responseSuccessLoggerFactory
} from '../src';
// EXAMPLE: INTIALIZING
const logger = new GLogger({ loggingMode: LoggingMode.LOCAL });
////#region HTTP logger express middleware
const successLogger = responseSuccessLoggerFactory(logger, 'MODULE', 'SUBMODULE', __filename);
const errorLogger = responseErrorLoggerFactory(logger, 'MODULE', 'SUBMODULE', __filename, false);
const server = express();
server.use(enhanceReqWithTransactionAndTime);
server.use(successLogger);
server.get('/success', (req, res, next) => {
res.send('success. see terminal for failure log examples');
next();
});
server.get('/fail', (req, res, next) => {
res.send('fail. see terminal for failure log examples');
res.status(400);
next(new Error('failure error'));
});
server.use(errorLogger);
const RANDOM_PORT = 57192;
server.listen(RANDOM_PORT, () => {
logger.info(`Server started on port ${RANDOM_PORT}`);
logger.info(`run npm example-server and make get calls to /success and /fail`);
});
//#endregion