-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (57 loc) · 1.79 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const express = require('express');
const { Client } = require('pg');
const cors = require('cors');
const app = express();
// Middleware
app.use(express.json());
app.use(cors());
// PostgreSQL client setup
const client = new Client({
host: 'localhost',
port: 5432,
user: 'user', // replace with your database username
password: 'pass', // replace with your database password
database: 'textdb',
});
client.connect()
.then(() => console.log('Connected to PostgreSQL'))
.catch((err) => console.log('Error connecting to PostgreSQL:', err));
// Define route to save text
app.post('/api/text', async (req, res) => {
const { content } = req.body;
const query = 'INSERT INTO texts(content) VALUES($1) RETURNING *';
const values = [content];
try {
const result = await client.query(query, values);
res.status(201).json(result.rows[0]);
} catch (err) {
console.error('Error saving text:', err);
res.status(400).send('Error saving text');
}
});
// Define route to get all texts
app.get('/api/texts', async (req, res) => {
try {
const result = await client.query('SELECT * FROM texts');
res.json(result.rows);
} catch (err) {
console.error('Error fetching texts:', err);
res.status(400).send('Error fetching texts');
}
});
// Define route to delete a text by ID
app.delete('/api/text/:id', async (req, res) => {
const { id } = req.params;
const query = 'DELETE FROM texts WHERE id = $1';
const values = [id];
try {
await client.query(query, values);
res.status(200).send(`Text with ID ${id} deleted.`);
} catch (err) {
console.error('Error deleting text:', err);
res.status(400).send('Error deleting text');
}
});
// Start server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));