-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (36 loc) · 1.05 KB
/
server.js
File metadata and controls
41 lines (36 loc) · 1.05 KB
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
const express = require('express');
const { Pool } = require('pg');
const app = express();
const port = process.env.PORT || 3000;
const pool = new Pool({
host: process.env.DB_HOST || 'db',
port: process.env.DB_PORT || 5432,
user: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD || 'postgres',
database: process.env.DB_NAME || 'testdb',
});
app.get('/', async (req, res) => {
try {
const result = await pool.query('SELECT * FROM items ORDER BY id');
res.send(`
<html>
<head><title>Test PG App</title></head>
<body>
<h1>Items</h1>
<ul>${result.rows.map(r => `<li>${r.name} - ${r.description}</li>`).join('')}</ul>
</body>
</html>
`);
} catch (err) {
res.status(500).send(`<h1>DB Error</h1><pre>${err.message}</pre>`);
}
});
app.get('/health', async (req, res) => {
try {
await pool.query('SELECT 1');
res.json({ status: 'ok' });
} catch {
res.status(503).json({ status: 'unhealthy' });
}
});
app.listen(port, () => console.log(`Listening on port ${port}`));