-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabases_server.js
39 lines (33 loc) · 938 Bytes
/
databases_server.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
const { Pool } = require('pg');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
app.post('/execute-sql', async (req, res) => {
const { host, user, password, database, port, query } = req.body;
const pool = new Pool({
host,
user,
password,
database,
port,
max: 1,
idleTimeoutMillis: 10000,
connectionTimeoutMillis: 10000, // return an error after 10 seconds if connection could not be established
});
try {
const client = await pool.connect();
const result = await client.query(query);
client.release();
res.json({ results: result.rows });
} catch (error) {
console.error('Error in /execute-sql:', error.message);
res.status(500).json({ error: error.message });
} finally {
await pool.end();
}
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});