-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
80 lines (67 loc) · 1.94 KB
/
server.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
require("dotenv").config;
//creating a server
const express = require("express");
//calling express function
const app = express();
const ejs = require("ejs");
const favicon = require("serve-favicon");
const path = require("path");
const expressLayout = require("express-ejs-layouts");
const {Module} = require("module");
const port = process.env.PORT || 3000;
const mongoose = require("mongoose");
const session = require("express-session");
const flash = require("express-flash");
const MongoDbStore = require("connect-mongo");
const passport = require("passport");
var bodyParser = require("body-parser");
const url =
"mongodb+srv://admin:tanmay28@cluster0.tcbwb.mongodb.net/pizza?retryWrites=true&w=majority";
mongoose.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const connection = mongoose.connection;
connection.once("open", () => {
console.log("Database connected...");
});
//Session Store
let mongoStore = MongoDbStore.create({
mongoUrl: url,
collection: "sessions",
});
app.use(express.json());
app.use(express.urlencoded({extended: false}));
//Session Config
//saving cookies in db
app.use(
session({
secret: "Hello",
resave: false,
store: mongoStore,
saveUninitialized: false,
cookie: {maxAge: 1000 * 60 * 60 * 24},
})
);
//passport config
const passportInit = require("./app/config/passport");
passportInit(passport);
app.use(passport.initialize());
app.use(passport.session());
//session
app.use(flash());
//Middleware
app.use((req, res, next) => {
res.locals.session = req.session;
res.locals.user = req.user;
next();
});
// set template engine
app.use(expressLayout);
app.set("views", path.join(__dirname, "/resources/views"));
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(favicon(path.join(__dirname, "public", "favicon.ico")));
//passing an instance
require("./routes/web")(app);
app.listen(port, () => console.log(`Example app listening on port ${port}`));