-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
109 lines (89 loc) · 2.47 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
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
const dotenv = require('dotenv');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const session = require('express-session');
const flash = require('connect-flash');
const mongoose = require('mongoose');
const socket = require('socket.io');
dotenv.config({
path: './config.env',
});
const server = require('./server');
// console.log('fuck', fuck);
const DB = process.env.DATABASE;
// const DB = process.env.DATABASE_LOCAL;
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then((con) => {
// console.log(con);
console.log('Database Connected Sccessfully');
});
// Routers
var index = require('./routes/indexRoutes');
var users = require('./routes/userRoutes');
var posts = require('./routes/postRoutes');
var categories = require('./routes/categoryRoutes');
const errorController = require('./controllers/errorController');
const AppError = require('./utils/appError');
var app = express();
// error handler
app.use(logger('dev'));
app.use(express.json());
app.use(cookieParser());
// view engine setup
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// app.set('views', `${__dirname}/views`);
// initialise session middleware - flash-express depends on it
app.use(
session({
secret: 'MySecret 344j ff ',
cookie: { maxAge: 600000 },
resave: false,
saveUninitialized: false,
})
);
app.use(flash());
app.use(express.static(`${__dirname}/public`));
// Routes
app.use(function (req, res, next) {
if (req.originalUrl && req.originalUrl.split('/').pop() === 'favicon.ico') {
return res.sendStatus(204);
}
return next();
});
// Routes
app.use('/dashboard', index);
app.use('/posts', posts);
app.use('/categories', categories);
app.use('/users', users);
app.get('/test', (req, res) => {
res.json({
success: 'true',
});
});
app.use('/', index);
// HANDLING UNHANDLED ROUTES
app.all('*', (req, res, next) => {
console.log('404');
next(new AppError(`Can't find ${req.originalUrl} on this server`, 404));
});
app.use(errorController);
server.appListen(app);
let socketServer = server.getServer();
// Socket.io Connection
io = socket(socketServer);
io.on('connection', (socket) => {
mySocket = socket;
// I want to sent this socket to every route Controller
console.log('connection made');
});
// console.log(s);
module.exports = { io };