-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (43 loc) · 1.28 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
// Import dependencies
const dotenv = require('dotenv');
// Configurations
dotenv.config();
const express = require('express');
const cors = require('cors');
const http = require('http');
const socketIo = require('socket.io');
const dbConnect = require('./config/dbConnect');
// Get dbConnect
dbConnect();
// Express and HTTP server setup
const app = express();
const server = http.createServer(app);
// Socket.io setup
const io = socketIo(server, {
cors: {
origin: ['http://localhost:3000'],
methods: ['GET', 'POST'],
},
});
// Middleware setup
app.use(cors());
app.use(express.json());
// import routes
const videoRoutes = require('./routes/videos');
const productRoutes = require('./routes/products');
const commentRoutes = require('./routes/comments');
const categoryRoutes = require('./routes/categories');
// Apply routes
app.use('/videos', videoRoutes);
app.use('/products', productRoutes);
app.use('/comments', commentRoutes);
app.use('/categories', categoryRoutes);
// Export io instance for use in routes
module.exports = io;
// Event Emitter setup
const myEmitter = require('./event');
myEmitter.on('newComment', (comments) => {
io.emit('comments', comments);
});
const port = process.env.PORT || 5000;
server.listen(port, () => console.log(`Server started on port ${port}`));