-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-js-version-1-ephemeral.txt
111 lines (92 loc) · 2.81 KB
/
server-js-version-1-ephemeral.txt
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(bodyParser.json());
app.use(cors());
// Add this line after cors() in server.js
app.use(express.static('public'));
// Add CSP middleware
app.use((req, res, next) => {
res.setHeader(
"Content-Security-Policy",
"default-src 'self'; script-src 'self' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline'; img-src 'self'"
);
next();
});
// Initialize SQLite database
const db = new sqlite3.Database(':memory:');
db.serialize(() => {
db.run('CREATE TABLE glossary (id INTEGER PRIMARY KEY, term TEXT, definition TEXT)');
});
// Add new entry
app.post('/save-entry', (req, res) => {
const { term, definition } = req.body;
db.run('INSERT INTO glossary (term, definition) VALUES (?, ?)', [term, definition], (err) => {
if (err) {
res.status(500).send({ message: 'Failed to save entry' });
} else {
res.status(200).send({ message: 'Entry saved successfully' });
}
});
});
// Get all entries
app.get('/entries', (req, res) => {
db.all('SELECT * FROM glossary', (err, rows) => {
if (err) {
res.status(500).send({ message: 'Failed to fetch entries' });
} else {
res.status(200).send(rows);
}
});
});
// Get a single entry by index
app.get('/entries/:index', (req, res) => {
const index = parseInt(req.params.index, 10);
db.all('SELECT * FROM glossary', (err, rows) => {
if (err || index < 0 || index >= rows.length) {
res.status(404).send('Not found');
} else {
res.json(rows[index]);
}
});
});
// Get specific entry
app.get('/get-entry/:id', (req, res) => {
const id = req.params.id;
db.get('SELECT * FROM glossary WHERE id = ?', [id], (err, row) => {
if (err) {
res.status(500).send({ message: 'Failed to fetch entry' });
} else {
res.status(200).send(row);
}
});
});
// Update entry
app.put('/update-entry/:id', (req, res) => {
const id = req.params.id;
const { term, definition } = req.body;
db.run('UPDATE glossary SET term = ?, definition = ? WHERE id = ?', [term, definition, id], (err) => {
if (err) {
res.status(500).send({ message: 'Failed to update entry' });
} else {
res.status(200).send({ message: 'Entry updated successfully' });
}
});
});
// Delete entry
app.delete('/delete-entry/:id', (req, res) => {
const id = req.params.id;
db.run('DELETE FROM glossary WHERE id = ?', [id], (err) => {
if (err) {
res.status(500).send({ message: 'Failed to delete entry' });
} else {
res.status(200).send({ message: 'Entry deleted successfully' });
}
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});