-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
96 lines (74 loc) · 2.26 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
const path = require('path');
const express = require('express');
const mongoose = require('mongoose');
const flash = require('connect-flash');
const bodyParser = require('body-parser');
const passport = require('passport');
const LocalStratergy = require('passport-local');
const dotenv = require('dotenv');
dotenv.config();
const keys = require('./keys');
const MONGODB_URI = keys.MONGODB_URI;
const app = express();
var http = require("http").createServer(app);
const port = process.env.PORT || 3000;
const server = app.listen(port , () => {
console.log(`Listening on port 3000`)
})
app.use(express.json());
app.set('view engine', 'ejs');
app.set('views', 'views');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
mongoose
.connect(MONGODB_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then((result) => {
console.log('database connected');
})
.catch((err) => {
console.log(err);
});
const User = require('./routeModels/user/User');
const Admin = require("./routeModels/admin/Admin")
app.use(
require('express-session')({
resave: false,
saveUninitialized: false,
secret: 'This is food shop',
})
);
app.use(flash());
app.use(function (req, res, next) {
res.locals.error = req.flash('error');
res.locals.success = req.flash('success');
next();
});
app.use(passport.initialize());
app.use(passport.session());
passport.use("user" ,new LocalStratergy(User.authenticate()))
passport.use("admin" ,new LocalStratergy(Admin.authenticate()))
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
if(user!=null)
done(null,user);
});
var indexRoutes = require('./routes/index');
var authRoutes = require('./routes/auth');
var productRoutes = require("./routes/product")
var adminRoutesAuth = require("./routes/adminAuth")
var adminIndexRoutes = require("./routes/adminRoutes")
var adminProductRoutes = require("./routes/adminProductRoutes")
app.use(indexRoutes);
app.use(authRoutes);
app.use(productRoutes)
app.use("/admin",adminRoutesAuth)
app.use("/admin",adminIndexRoutes)
app.use(adminProductRoutes)