-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (55 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
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
// Middleware
app.use(bodyParser.json());
app.use(express.static('public')); // Serve static files from the 'public' directory
// Connect to SQLite database
let db = new sqlite3.Database('./chatbot.db', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the SQLite database.');
});
// Create tables
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS Users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL,
email TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS Messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
message TEXT NOT NULL,
sender TEXT NOT NULL, -- 'user' or 'ai'
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES Users (id)
)`);
});
// Endpoint to save messages
app.post('/api/messages', (req, res) => {
const { user_id, message, sender } = req.body;
db.run(`INSERT INTO Messages (user_id, message, sender) VALUES (?, ?, ?)`, [user_id, message, sender], function(err) {
if (err) {
return res.status(500).json({ error: err.message });
}
res.json({ id: this.lastID });
});
});
// Endpoint to get messages
app.get('/api/messages', (req, res) => {
db.all(`SELECT * FROM Messages ORDER BY timestamp ASC`, [], (err, rows) => {
if (err) {
return res.status(500).json({ error: err.message });
}
res.json(rows);
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});