-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.ts
106 lines (85 loc) · 2.24 KB
/
example.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import express from "express";
import Boom from "@hapi/boom";
import winston from "winston";
import chrona from "./index";
const app = express();
const router = express.Router();
class Logger {
output: winston.Logger;
public constructor() {
this.output = winston.createLogger({
level: "info",
});
this.output.add(
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.printf((info) => {
const { message, level } = info;
return `${level}: ${message}`;
}),
),
}),
);
}
public info(message: string) {
this.output.info(message);
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const logger = new Logger();
// winston as transporter
// app.use(
// chrona(
// ":[date] :incoming :method :url :status :response-time :content-length :user-agent :http-version",
// (str) => logger.info(str),
// ),
// );
// default format and transporter
app.use(
chrona(
":date :incoming :method :url :status :response-time :content-length :user-agent :http-version",
),
);
function timeout(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
router.get("/users", async (_, res) => {
await timeout(1000);
return res.status(200).json({
message: "Users fetched successfully",
users: Array.from({ length: 200 }, (_, i) => ({
id: i,
})),
});
});
router.post("/users", (_, res) => {
return res.status(201).json({
message: "Users created successfully",
});
});
router.get("/200", (req, res) => {
return res.status(200).send("OK");
});
router.get("/301", (req, res) => {
return res.status(301).send("Moved Permanently");
});
router.get("/304", (req, res) => {
return res.status(304).send("Not Modified");
});
router.get("/404", (req, res) => {
return res.status(404).send("Not Found");
});
router.get("/500", (req, res) => {
return res.status(500).send("Internal Server Error");
});
router.get("/500-boom", () => {
throw Boom.badImplementation("Bad implementation");
});
router.get("/error", () => {
throw new Error("Error");
});
app.use(router);
app.listen(3000, () => {
console.log("Server started on port 3000");
});