-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathapp.js
More file actions
33 lines (28 loc) · 850 Bytes
/
app.js
File metadata and controls
33 lines (28 loc) · 850 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
const express = require('express');
const { Pool } = require('pg');
const app = express();
const port = process.env.PORT || 3000;
// PostgreSQL connection configuration
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
});
// Middleware to parse JSON requests
app.use(express.json());
// Example route to get data from the database
app.get('/data', async (req, res) => {
try {
const result = await pool.query('SELECT * FROM your_table_name');
res.status(200).json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Server error');
}
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});