-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
40 lines (33 loc) · 1.02 KB
/
app.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
import express, { Express, Request, Response, NextFunction } from "express";
import templateRoutes from "./src/api/routes/templateRoute";
import winston from "winston";
const app: Express = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
const logger = winston.createLogger({
level: "info",
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: "logs.log" }),
],
});
app.use((req: Request, res: Response, next: NextFunction) => {
logger.info(`Received ${req.method} request for ${req.url}`);
next();
});
app.use("/api/templates", templateRoutes);
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
logger.error(`Error: ${err.message}`, {
method: req.method,
url: req.url,
errorStack: err.stack,
});
res.status(500).send("Something broke!");
});
app.listen(PORT, () => {
console.log(`Server runs on port ${PORT}`);
});