-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (66 loc) · 1.71 KB
/
app.js
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
const express = require("express");
const axios = require("axios");
const mysql = require("mysql2");
const redis = require("redis");
const { trace } = require("@opentelemetry/api");
const mysqlClient = mysql.createConnection({
host: "mysql",
user: "root",
password: "root",
database: "test",
});
mysqlClient.connect((err) => {
if (err) {
if (process.env.CUBE_DOCKER_COMPOSE) throw err;
} else {
console.log("mysql connected!");
}
});
const redisClient = redis.createClient({
url: "redis://redis:6379",
});
redisClient.connect().catch((err) => {
if (err) {
if (process.env.CUBE_DOCKER_COMPOSE) throw err;
} else {
console.log("redis connected!");
}
});
const PORT = parseInt(process.env.PORT || "8000");
const app = express();
app.get("/", (req, res) => {
res.send("Hello");
});
app.get("/param/:param", function (req, res) {
res.send("Got param " + req.params.param);
});
app.get("/exception", function (req, res) {
throw new Error("Sample exception");
});
app.get("/api", (req, res) => {
axios
.get("http://localhost:8000/")
.then((response) => res.send("API called"));
});
app.get("/mysql", (req, res) => {
mysqlClient.query("SELECT NOW()", (err, results, fields) => {
res.send(results[0]["NOW()"]);
});
});
app.get("/redis", (req, res) => {
redisClient.set("foo", "bar");
res.send("Redis called");
});
const errorHandler = (err, req, res, next) => {
const span = trace.getActiveSpan();
if (span) {
span.recordException(err);
}
// pass the error to the next middleware
// you can do any custom error handling here
next(err);
};
app.use(errorHandler);
app.listen(PORT, () => {
console.log(`Listening for requests on http://localhost:${PORT}`);
});