-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
56 lines (47 loc) · 1.16 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
import express from "express";
import dotenv from "dotenv";
import { scheduler } from "./lib/arbox.js";
const app = express();
dotenv.config();
app.use(express.json());
// Setting CORS Headers to every response of the server
app.use((req, res, next) => {
res.setHeader(
"Access-Control-Allow-Origin","*"
); // * => this is the domain
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, DELETE, OPTIONS"
);
next();
});
// Initiate Arbox scheduler
await scheduler();
app.get("/", async (req, res, next) => {
console.log("Health check 🩸🧬");
const healthcheck = {
uptime: process.uptime(),
message: "OK",
timestamp: Date.now(),
};
try {
console.log(healthcheck);
res.status(200).send(healthcheck);
} catch (error) {
healthcheck.message = error;
res.status(503).send();
}
});
app.use((error, req, res, next) => {
if (res.headerSent) {
return next(error);
}
res.status(error.code || 500);
res.json({ message: error.message || "An unknown error occured!" });
});
// listen to requests
app.listen(5000);