-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
43 lines (35 loc) · 820 Bytes
/
Copy pathapp.js
File metadata and controls
43 lines (35 loc) · 820 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
const express = require('express');
const { Pool } = require('pg');
const app = express();
app.use(express.json());
const pool = new Pool({
user: 'db',
host: 'localhost',
database: 'db',
password: '123456',
port: 5432,
});
// ❌ REMOVE auto query from top
// ✅ Create function instead
async function initDB() {
await pool.query(`
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name TEXT
)
`);
}
// routes
app.get('/', (req, res) => {
res.send("Node + PostgreSQL App ");
});
module.exports = app;
// ✅ Only run DB + server when app is started directly
if (require.main === module) {
const port = process.env.PORT || 3000;
initDB().then(() => {
app.listen(port,'0.0.0.0', () => {
console.log(`Server running on IPv4 0.0.0.0:3000`);
});
});
}