-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (58 loc) · 1.79 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
const express = require("express");
//const bodyParser = require("body-parser");
const expressLauout = require("express-ejs-layouts");
const database = require("./database/database");
const router = require("./routes/product.routes");
const flash = require("connect-flash");
const session = require("express-session");
const passport = require("passport");
const localStrategy = require("./config/localstrategy.passport")
const User = require("./models/users.model");
// Initiating Express app
const app = express();
// Express Session
app.set('trust proxy', 1); // trust first proxy
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
}));
// Passport
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done){
done(null, user.id)
});
passport.deserializeUser(function(id,done){
User.findById(id, function(err, user){
done(err, user);
});
});
passport.use(localStrategy);
// Connect flash
app.use(flash());
// Global Variables
app.use(function(req,res,next){
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
next();
});
// EJS
app.use(expressLauout);
app.set("view engine", "ejs");
// Body Parser
app.use(express.json());
app.use(express.urlencoded({extended: false}));
// Connect Database
database.connect();
// Routes
app.use("/users", require("./routes/users.routes"));
app.use("/products", require("./routes/product.routes"));
app.use("/", require("./routes/index.router"));
app.use(express.static(__dirname + '/public'));
//dedicating a port number and listening to that port
let port = process.env.PORT || 5000;
app.listen(port, function(){
console.log("Server is up and running on port " + port);
});