-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
108 lines (89 loc) · 2.75 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
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
97
98
99
100
101
102
103
104
105
106
107
108
if (process.env.NODE_ENV !== "production") {
require('dotenv').config();
}
// Import modules
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const session = require('express-session');
const flash = require('connect-flash');
const methodOverride = require('method-override');
const MongoDBStore = require('connect-mongo')(session);
const { check } = require('express-validator');
const passport = require('passport');
const localStrategy = require ('passport-local');
const app = express();
// Import models
const Product = require("./models/product");
const Cart = require("./models/cart");
const Comment = require("./models/comment");
const User = require("./models/user");
// Import routes
const indexRoutes = require("./routes/index");
const productRoutes = require("./routes/products");
const commentRoutes = require("./routes/comments");
// const cartRoutes = require("./routes/carts");
// MongoDB configuration
const dbUrl = process.env.DB_URL;
//! Remote Database URL
// const dbUrl = 'mongodb://0.0.0.0:27017/kids-place';
mongoose.connect(dbUrl, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
}).then(() => {
console.log("Database Connected");
}).catch(error => {
console.log('ERROR:', error.message);
});
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
// Session & MongoStore configuration
const secret = process.env.SECRET || 'Cinnamon buns';
const store = new MongoDBStore({
url: dbUrl,
secret,
touchAfter: 24 * 60 * 60 // lazy session update time period in seconds
});
store.on("error", function (e) {
console.log("Session Store Error", e);
})
const sessionConfig = {
store,
secret,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
// secure: true,
expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
maxAge: 1000 * 60 * 60 * 24 * 7
}
};
app.use(session(sessionConfig));
// Flash & Passport configuration
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use((req, res, next) => {
console.log(req.session)
res.locals.currentUser = req.user;
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
next();
})
// Routes configuration
app.use(indexRoutes);
app.use(productRoutes);
app.use(commentRoutes);
// app.use(cartRoutes);
const port = process.env.PORT || 3008;
app.listen(port, () => {
console.log(`Kid's Place app serving on port ${port}`)
});