-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (39 loc) · 1014 Bytes
/
index.js
File metadata and controls
47 lines (39 loc) · 1014 Bytes
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
const express = require("express");
const app = express();
const log = (str) => {
console.log(`cloudRunHello: ${str}`);
};
let preparingExit = false;
const prepareExit = async (signal) => {
if (preparingExit) return;
preparingExit = true;
log("\nPreparing exit with signal: " + signal);
process.exit();
};
process.on("exit", (code) => {
if (code || code === 0) log("exitCode", code);
});
["SIGINT", "SIGTERM", "SIGUSR1", "SIGUSR2"].forEach((signal) => {
process.on(signal, prepareExit);
});
//catches uncaught exceptions
process.on("uncaughtException", (e) => {
log("[uncaughtException] app will be terminated");
console.error(e);
prepareExit("uncaughtException");
});
app.get("/", (req, res) => {
log("ok");
res.send("Hello World!");
});
app.get("/error", (req, res) => {
log("error");
res.send("error");
setTimeout(() => {
throw new Error("APP ERROR");
}, 0);
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
log(`listening on port ${port}`);
});