-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (54 loc) · 1.74 KB
/
index.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
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var ejs = require('ejs');
var app = express();
app.use(express.static('public'));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/chatbot', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => console.log('MongoDB connected'))
.catch(err => console.log(err));
// Define a schema
const MessageSchema = new mongoose.Schema({
userMessage: String,
chatbotResponse: String,
timestamp: { type: Date, default: Date.now }
});
const Message = mongoose.model('Message', MessageSchema);
// Routes
app.get('/', function (req, res) {
res.render('pages/index');
});
app.get('/about', function (req, res) {
res.render('pages/about');
});
app.get('/contact', function (req, res) {
res.render('pages/contact');
});
// Route to save messages
app.post('/messages', async (req, res) => {
const { userMessage, chatbotResponse } = req.body;
const message = new Message({ userMessage, chatbotResponse });
await message.save();
res.send(message);
});
// Route to get chatbot response
app.post('/get-response', async (req, res) => {
const { userMessage } = req.body;
const message = await Message.findOne({ userMessage: new RegExp(`^${userMessage}$`, 'i') });
if (message) {
res.send({ chatbotResponse: message.chatbotResponse });
} else {
res.send({ chatbotResponse: "I didn't understand that." });
}
});
const port = 8080;
app.listen(port, () => {
console.log(`Server is live on port ${port}!`);
}).on('error', (err) => {
console.error(`Failed to start server: ${err}`);
});