-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (46 loc) · 1.36 KB
/
server.js
File metadata and controls
54 lines (46 loc) · 1.36 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
42
43
44
45
46
47
48
49
50
51
52
53
54
import express from "express";
import dotenv from "dotenv";
import pg from "pg";
import authRoutes from './routes/authRoutes.js';
import bookRoutes from './routes/bookRoutes.js';
import reviewRoutes from './routes/reviewRoutes.js';
dotenv.config();
export const pool = new pg.Pool({
database: process.env.PSQL_DB_NAME,
user: process.env.PSQL_DB_USER,
host: process.env.PSQL_DB_HOST,
password: process.env.PSQL_DB_PASSWORD,
port: process.env.PSQL_DB_PORT
});
pool.connect((err, client, release) => {
if (err) {
return console.error('Error acquiring client:', err.stack);
}
client.query('SELECT NOW()', (err, result) => {
release();
if (err) {
return console.error('Error executing query', err.stack);
}
console.log('Connected to PostgreSQL database');
});
});
const app = express();
app.use(express.json());
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something went wrong!' });
});
app.use('/api/auth', authRoutes);
app.use('/api/books', bookRoutes);
app.use('/api', reviewRoutes);
const PORT = process.env.PORT || 8080;
pool.connect()
.then(() => {
app.listen(PORT, () => {
console.log(`Server is running on port http://localhost:${process.env.PORT}`);
});
})
.catch((err) => {
console.error('Failed to connect to the database:', err);
process.exit(1);
});