-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
119 lines (96 loc) · 3.14 KB
/
server.js
File metadata and controls
119 lines (96 loc) · 3.14 KB
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
112
113
114
115
116
117
118
const express = require('express');
const routes = require('./routes');
const exphbs = require('express-handlebars');
const path = require('path');
const session = require('express-session');
const helpers = require('./utils/helpers');
const Chat = require('./models/Chat');
// import sequelize connection
const sequelize = require('./config/connection');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const app = express();
const PORT = process.env.PORT || 3001;
// Required for the socket.io server
const http = require('http');
const socketIo = require('socket.io');
const server = http.createServer(app);
const io = socketIo(server);
const formatMessage = require('./utils/messages');
const { ChatMessage } = require('./models');
// Set up Handlebars.js engine with custom helpers
const hbs = exphbs.create({ helpers });
const sess = {
secret: 'Super secret secret',
cookie: {
maxAge: 24 * 60 * 60 * 1000,
httpOnly: true,
secure: false,
sameSite: 'strict',
},
resave: false,
saveUninitialized: true,
store: new SequelizeStore({
db: sequelize
})
};
const createChatMessage = async (text, user_id) => {
try {
await Chat.create({
text: text,
user_id: user_id
});
} catch (error) {
console.error(error);
}
};
app.use(session(sess));
// Starts the server for socket.io
server.listen(PORT, () => {
console.log(`Server listening on: http://localhost:3000`);
});
const botName = 'Chat Bot';
io.on('connection', async socket => {
try {
// Get the 10 most recent chat messages from the database
const messages = await Chat.findAll({
limit: 10,
order: [['time_stamp', 'DESC']]
});
// Emit each message to the client
messages.reverse().forEach(message => {
socket.emit('message', formatMessage('USER', message.text));
});
} catch (error) {
console.error(error);
}
socket.emit('message', formatMessage(botName, `Welcome to the chat!`))
socket.broadcast.emit('message', formatMessage(botName, 'A user has joined the chat'));
socket.on('disconnect', () => {
socket.emit('message', formatMessage('USER', 'A user has left the chat'));
})
socket.on('chatMessage', async (msg) => {
io.emit('message', formatMessage('USER', msg));
try {
// Save the message to the database
await createChatMessage(msg);
} catch (error) {
console.error(error);
}
});
});
// Inform Express.js on which template engine to use
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// app.use(express.static('public'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(routes);
sequelize.sync({ force: false }).then(() => {
console.log('Database Synced');
// app.listen(PORT, () => console.log(`Now Listening on port ${PORT}`));
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Server Error');
});