-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloggingServer.ts
More file actions
75 lines (61 loc) · 2.12 KB
/
Copy pathloggingServer.ts
File metadata and controls
75 lines (61 loc) · 2.12 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
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
import * as bodyparser from 'body-parser';
import * as express from 'express';
import * as fileUpload from 'express-fileupload';
import * as winston from 'winston';
import * as expressWinston from 'express-winston';
import * as cors from 'cors';
import debug from 'debug';
import { CommonRoutesConfig } from './common/common-routes.config';
import { DataRoutes } from './routes/data.routes.config';
import { LoggingRoutes } from './routes/logging.routes.config';
const app = express();
const port: Number = 4302;
const routes: Array<CommonRoutesConfig> = [];
const debugLog: debug.IDebugger = debug('app');
const logAllHttpRequests = false;
let name = new Date().toISOString();
name = name.replace(/[/\\?%*:|"<>]/g, '');
app.use(express.static('data'));
app.use(express.static('logs'));
// here we are adding middleware to parse all incoming requests as JSON
app.use(bodyparser.json({limit: '200mb'}));
// here we are adding middleware to allow cross-origin requests
app.use(cors());
// enable file upload middleware
app.use(fileUpload());
// here we are configuring the expressWinston logging middleware,
// which will automatically log all HTTP requests handled by Express.js
if (logAllHttpRequests) {
app.use(expressWinston.logger({
transports: [
new winston.transports.Console()
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
)
}));
}
// here we are configuring the expressWinston error-logging middleware,
// which doesn't *handle* errors per se, but does *log* them
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({
filename: `data/log/${name}_error.log`
})
],
format: winston.format.combine(
winston.format.simple()
)
}));
app.listen(port, () => {
const msg = `Server running at http://localhost:${port}`;
debugLog(msg);
routes.forEach((route: CommonRoutesConfig) => {
debugLog(`Routes configured for ${route.getName()}`);
});
});
routes.push(new DataRoutes(app));
routes.push(new LoggingRoutes(app));
export { app };