-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (66 loc) · 1.84 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
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const session = require('express-session');
const MongoDbStore = require('connect-mongodb-session')(session)
const { MONGO_URI } = require('./db/database');
const csrf = require('csurf');
// importing routes
const home = require('./routes/landing');
const addProduct = require('./routes/addProduct');
const categoryPage = require('./routes/category');
const cart = require('./routes/cart');
const login = require('./routes/login');
const signup = require('./routes/signup');
const logout = require('./routes/logout');
const app = express();
app.set('view engine', 'ejs');
app.set('views', 'views')
//creating a session storage in database
const store = new MongoDbStore({
uri: MONGO_URI,
collection: 'sessions'
})
app.use(express.urlencoded({extended: false}))
//serving static files like css
app.use(express.static(path.join(__dirname, 'public')))
// session middleware
app.use(session({
secret: 'mysecret',
resave: false,
saveUninitialized: false,
store: store,
cookie: {
httpOnly: true,
maxAge: 3600000
}
}))
// csrf middleware
app.use(csrf());
app.use((req, res, next) => {
res.locals.Authenticated = req.session.isLoggedin;
res.locals.csrfToken = req.csrfToken();
next();
})
app.use(home);
app.use(login);
app.use(signup);
app.use(categoryPage);
//logout on session expire
app.use((req, res, next)=> {
if(!req.session.isLoggedin){
return res.redirect('/login')
}
next();
});
app.use(addProduct);
app.use(cart);
app.use(logout);
// 404
app.use((req, res) => {
res.send("Page Not Found")
})
//connecting database
mongoose.connect(MONGO_URI, {useNewUrlParser: true, useUnifiedTopology:true}).then(result => {
app.listen(3000);
}).catch(err => console.log("connection failed", err))