-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (93 loc) · 3.09 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
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
// THIS FILE WILL CONTAIN CONFIGURATION WHICH IS RELATED TO EXPRESS
const path = require('path');
const express = require('express');
// const pug = require('pug');
const bodyParser = require('body-parser');
const http = require('http');
const socketIO = require('socket.io');
const cookieParser = require('cookie-parser');
const app = express();
const userRouter = require('./routes/userRoutes');
const groupRouter = require('./routes/groupRoutes');
const viewRouter = require('./routes/viewRoutes');
const AppError = require('./utils/appError');
app.set('view engine', 'pug');
// __dirname -> current directory
app.set('views', path.join(__dirname, 'views'));
// FOR GOING TO THE EXACT PATH, IN THE PUG, WE ARE MOVING CURRENT DIR TO PUBLIC
app.use(express.static(path.join(__dirname, '/public')));
// express.json() is a middleware, it parses the json in the incoming req object
app.use(express.json());
// doubt
app.use(express.urlencoded({ extended: true }));
// IT ENABLES US TO USE DATA RECEIVED IN REQ.BODY
app.use(
bodyParser.urlencoded({
extended: true,
})
);
// TO ACCESS COOKIE AT BACKEND
app.use(cookieParser());
// MOUNTING ROUTERS
app.use('/api/users', userRouter);
app.use('/api/groups', groupRouter);
app.use('/', viewRouter);
// A MIDDLEWARE WHICH RUNS A UNIDENFIED ROUTE IS CALLED
app.all('*', (req, res, next) => {
next(new AppError(`Can't Find ${req.originalUrl} on this server!`, 404));
});
// Catching Errors -> a very basic error handler
app.use((err, req, res, next) => {
console.log('Inside Error Handler function....');
err.statusCode = err.statusCode || 500;
err.status = err.status || 'error';
console.log(err);
// WHEN DATA IS REQUESTED THROUGH API
if (req.originalUrl.startsWith('/api')) {
res.status(err.statusCode).json({
err: err.status,
errror: err,
message: err.message,
stack: err.stack,
});
}
// WHEN PAGE IS REQUESTED
else {
if (err.statusCode === 401) {
return res.status(err.statusCode).render('login', {
title: 'Login to Continue!!',
});
}
res.status(err.statusCode).render('error', {
title: 'Something went wrong!!',
message: err.message,
});
}
});
// END OF ERROR HANDLER FUNC
// Adding Socket IO for chatRoom thing
const server = http.createServer(app);
const io = socketIO(server);
io.on('connection', (socket) => {
socket.on('new_user_joined', (data) => {
// Creating room for this group id
socket.join(data.groupId);
// sockets.in(data.groupId).emit('user_joined', data.user); //emits to all
socket.broadcast.to(data.groupId).emit('user_joined', data);
});
socket.on('is-typing', (data) => {
socket.join(data.groupId);
socket.broadcast.to(data.groupId).emit('is-typing', data);
});
socket.on('msg-send', (data) => {
socket.join(data.groupId);
// sockets.in(data.groupId).emit('msg-receive', data);
socket.broadcast.to(data.groupId).emit('msg-receive', data);
});
});
const port = process.env.PORT || 4000;
server.listen(port, (req, res) => {
console.log(`Running Server on port ${port}`);
});
// end of chatroom
module.exports = app;