-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
45 lines (40 loc) · 1.09 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
var express = require("express");
var app = express();
const PG_CONFIG = {
user: process.env.DB_USER,
host: process.env.DB_HOST.match(/(.*):5432/)
? process.env.DB_HOST.match(/(.*):5432/)[1]
: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASS,
port: 5432,
};
app.get("/", function (req, res) {
res.send("Hello world!");
});
app.get("/health", async function (_, res) {
res.sendStatus(200);
});
app.get("/db_healthcheck", async function (_, res) {
const { Pool } = require("pg");
const pool = new Pool(PG_CONFIG);
const text = `
CREATE TABLE IF NOT EXISTS "users" (
"id" SERIAL,
"name" VARCHAR(100) NOT NULL,
"role" VARCHAR(15) NOT NULL,
PRIMARY KEY ("id")
);`;
try {
await pool.query(text);
} catch (error) {
console.log("already created");
console.log(error);
}
const d_res = await pool.query("SELECT * FROM users LIMIT 5");
pool.end();
return res.send(d_res);
});
app.listen(process.env.PORT || "3000", "0.0.0.0", 10, () => {
console.log("app is working on `http://localhost:3000/`...");
});