-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
28 lines (20 loc) · 866 Bytes
/
server.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
import 'dotenv/config';
import app from './app';
import { Request, Response, NextFunction } from 'express';
import { v4 as uuidv4 } from 'uuid';
import logger from './src/config/logger'
const PORT: number | string = process.env.PORT || 3000;
app.listen(PORT, () => {
logger.info(`SERVER RUNNING ON PORT ${PORT}`);
});
app.use((request: Request, response: Response, next: NextFunction) => {
const requestId = uuidv4();
request.headers['x-request-id'] = requestId;
const start = Date.now();
logger.info(`Request iniciado - ID: ${requestId}, Método: ${request.method}, URL: ${request.url}`);
response.on('finish', () => {
const duration = Date.now() - start;
logger.info(`Request finalizado - ID: ${requestId}, Método: ${request.method}, URL: ${request.url}, Status: ${response.statusCode}, Duração: ${duration}ms`);
});
next();
})