-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (58 loc) · 1.58 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
import express from 'express';
import mongoose from 'mongoose';
import session from 'express-session';
import MongoStore from 'connect-mongo';
import flash from 'connect-flash';
import * as dotenv from 'dotenv';
import methodOverride from 'method-override';
import pageRouter from './routes/pageRoute.js';
import courseRouter from './routes/courseRoute.js';
import categoryRouter from './routes/categoryRoute.js'
import userRouter from './routes/userRoute.js'
dotenv.config();
const app = express();
const port = 3000;
//Connect DB
mongoose.connect('mongodb://localhost/smartedu-db')
.then(() => {
console.log("DB Connect Successfuly");
})
.catch((err) => {
console.log(err);
});
//Template Engine
app.set("view engine", "ejs");
//Global Veriables
global.userIN = null;
//Middlewares
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
secret: 'my keyboard cat',
resave: false,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: 'mongodb://localhost/smartedu-db' })
}));
app.use(flash());
app.use((req, res, next) => {
res.locals.flashMessages = req.flash();
next();
});
app.use(
methodOverride('_method', {
methods: ['POST', 'GET'],
})
);
//Routes
app.use('*', (req, res, next) => {
userIN = req.session.userID;
next();
});
app.use('/', pageRouter);
app.use('/courses', courseRouter);
app.use('/categories', categoryRouter);
app.use('/users', userRouter);
app.listen(port, () => {
console.log(`App started on port ${port}.`);
});