-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (60 loc) · 1.63 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
69
70
71
72
73
74
const express = require("express")
const app = express()
const {Sequelize} = require("sequelize")
const Chat = require("./models/Chat")
app.use(express.json())
app.get('/', (req, res) => {
res.status(200).json({
message: "welcome"
})
})
app.get('/allchats', async (req, res) => {
const allchats = await Chat.findAll();
res.status(200).json({
data: allchats
})
})
app.post('/createchat', (req, res) => {
const chat = Chat.build({ question: req.body.question, answer: req.body.answer});
(async function save(){
await chat.save();
console.log(chat);
console.log('Chat was saved to the database!');
})()
res.status(200).json({
message: "created"
})
})
app.patch('/updatechat', (req, res) =>{
})
app.delete('/deletechat', (req, res) =>{
})
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'db/database.sqlite'
});
(async function connect(){
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
})()
app.listen(3000, () => {
console.log('server is running on port 3000')
})
app.post('/createchat', async (req, res) => {
try {
const chat = await Chat.create({ question: req.body.question, answer: req.body.answer });
console.log('Chat was saved to the database:', chat);
res.status(200).json({
message: "created"
});
} catch (error) {
console.error('Error while creating chat:', error);
res.status(500).json({
message: "error"
});
}
});