-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
83 lines (60 loc) · 2 KB
/
app.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
75
76
77
78
79
80
81
82
83
const express = require("express")
const app = express()
app.use(express.urlencoded({ extended: false }))
const dotenv = require('dotenv').config()
const session = require("express-session")
const sequelize = require("./data/db")
const SequelizeStore = require("connect-session-sequelize")(session.Store);
// socket
const http = require("http")
const server = http.createServer(app)
const socketio = require("socket.io")
const io = socketio(server)
io.on('connection', (socket) => {
console.log(`Socket connected: ${socket.id}`);
socket.on('joinRoom', (roomId) => {
socket.join(roomId);
console.log(`Socket ${socket.id} joined room: ${roomId}`);
});
socket.on('message', (messageData) => {
console.log(`Message received: ${messageData.message, messageData.roomId}`);
io.to(messageData.roomId).emit('message', messageData);
});
socket.on('disconnect', () => {
console.log(`Socket disconnected: ${socket.id}`);
});
});
const cookieParser = require('cookie-parser')
app.use(cookieParser())
const bodyParser = require("body-parser")
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(session({
secret: 'communication portal',
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 60 * 60 * 1000 // 60 min
},
store: new SequelizeStore({
db: sequelize
})
}))
const locals = require("./middlewares/locals")
app.use(locals);
const authRoutes = require("./routers/auth")
const userRoutes = require("./routers/user")
app.set("view engine", "ejs")
app.use(express.static('public/css'));
app.use("/public", express.static("public")); // makes static files reachable under public folder as middleware
app.use("/auth", authRoutes)
app.use("/", userRoutes)
const testData = require("./data/test-data")
async function sync() {
await sequelize.sync({ alter: true })
// await testData()
}
sync()
server.listen(process.env.PORT, () => {
console.log("sunucu 3000 de aktif")
})