-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (62 loc) · 2 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
const express = require('express');
const path = require('path');
const bodyparser=require('body-parser');
const productRoutes=require('./routes/product');
const shopRoutes=require('./routes/shop.js');
const adminRoutes=require('./routes/admin.js');
const authRoutes=require('./routes/auth.js');
const errorController=require('./controllers/errorController.js');
// const mongoConnect=require('./util/database.js').mongoConnect;
const mongoose = require('mongoose')
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const User = require('./Models/user.js');
const MONGODB_URI ='mongodb://localhost:27017/NJD'
const app= express();
const store = new MongoDBStore({
uri: MONGODB_URI,
collection: 'sessions'
});
//parsing incoming Post request data
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended: false}));
//setting "static" folder as static files location
app.use("/static",express.static(path.join(__dirname,"static")));
app.use(
session({
secret: 'my secret',
resave: false,
saveUninitialized: false,
store: store
})
);
//setting templating engine and views details and connecctions
app.set("view engine","EJS");
app.set('views', __dirname + '/views');
app.set('view options', { basedir: process.env.__dirname})
//user Middelware
app.use((req, res, next) => {
if (!req.session.user) {
return next();
}
User.findById(req.session.user._id)
.then(user => {
req.user = user;
next();
})
.catch(err => console.log(err));
});
//registering all routes
app.use("/admin",adminRoutes);
app.use("/shop",shopRoutes);
app.use(authRoutes);
app.use("/product",productRoutes);
app.use(errorController.get404);
mongoose.connect(MONGODB_URI).then(result=>{
app.listen(process.env.port,err =>{
if(err)
console.log(err);
else
console.log("Server Started")
})
})