forked from rituparna-ui/eligram-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (103 loc) · 2.81 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
// ! Core Modules imports
const path = require('path');
// ! NPM Modules imports
const express = require('express');
const cors = require('cors');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const flash = require('connect-flash');
const multer = require('multer');
// ! Util Imports
const DB_CONNECT = require('./utils/db');
const User = require('./models/user.model');
// ! App settings
const app = express();
const PORT = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.set('views', 'views');
const store = MongoDBStore({
uri: process.env.A_DB_URI,
collection: 'sessionstorecollection',
});
const fsStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'assets/user/uploads/images/temp');
},
filename: (req, file, cb) => {
cb(null, Date.now() + file.originalname);
},
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === 'image/jpeg' ||
file.mimetype === 'image/png' ||
file.mimetype === 'image/gif' ||
file.mimetype === 'image/bmp'
) {
cb(null, true);
} else {
cb(null, false);
}
};
// ! App Middlewares
app.use(cors());
app.use('/assets', express.static(path.join(__dirname, 'assets')));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(
session({
cookie: {
maxAge: 12 * 60 * 60 * 1000,
},
secret: 'qetuoadgjl',
resave: false,
saveUninitialized: false,
store: store,
})
);
app.use(multer({ storage: fsStorage, fileFilter }).single('image'));
app.use(flash());
app.use(async (req, res, next) => {
// console.log(req.session.loggedin);
if (req.session.loggedin) {
// console.log(req.session.username);
// console.log(req.session.uid);
const tempUser = await User.findOne({ _id: req.session.uid });
req.user = tempUser;
}
next();
});
// app.use((req, res, next) => {
// console.log('logging hueheu');
// console.log(req?.user);
// next();
// });
// ! Route Imports
const authRoutes = require('./routes/auth');
const homeRoutes = require('./routes/home');
const postRoutes = require('./routes/post');
const searchRoutes = require('./routes/search');
const asyncRoutes = require('./routes/async');
// ! Route Middlewares
app.use('/auth', authRoutes);
app.use('/', homeRoutes);
app.use('/', postRoutes);
app.use('/', searchRoutes);
app.use('/async', asyncRoutes);
// ! Test Route
app.get('/test', (req, res) => {
res.render('login', {
accountCreated: '',
});
});
DB_CONNECT("mongodb+srv://elicon:[email protected]/?retryWrites=true&w=majority")
.then(() => {
console.log(`DB Connected`);
return app.listen(PORT);
})
.then(() => {
console.log(`Server Started at ${PORT}`);
})
.catch((err) => {
console.log('Could not connect to database');
});