This repository has been archived by the owner on Jul 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.js
79 lines (66 loc) · 2.11 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
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const expressLayouts = require("express-ejs-layouts");
const session = require("express-session");
const flash = require("connect-flash"); // displays on time msgs
const passport = require("./helper/ppConfig");
const methodOverride = require('method-override');
const MongoStore = require('connect-mongo')(session)
//init
const server = express()
//Routers
const authRoutes = require("./routes/auth.route");
const itemRoutes = require("./routes/item.route");
const orderRoutes = require("./routes/order.route");
//
//mongoDB connection
mongoose.connect(process.env.MONGODB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
}, () => console.log(`Connected to ${process.env.MONGODB}`), (err) => console.log(`MongoDB ERROR: ${err}`))
mongoose.set("debug", true);
server.use(express.static("public")); //tells express to look in public for static files
server.use(express.urlencoded({
extended: true
}))
server.set("view engine", "ejs")
server.use(expressLayouts)
server.use(methodOverride('_method'));
//--must be before passport
server.use(
session({
secret: process.env.SECRET,
saveUninitialized: true,
resave: false,
// cookie: { maxAge: 360000 } //duration of session
store: new MongoStore({
url: process.env.MONGODB
})
})
)
server.use(passport.initialize())
server.use(passport.session())
server.use(flash())
server.use(function (request, response, next) {
response.locals.alerts = request.flash() //displays one time messages
response.locals.currentUser = request.user
console.log(response.locals.alerts)
next()
})
//server.use(<routes>)
server.use(authRoutes)
server.use(itemRoutes)
server.use(orderRoutes)
//
//must be after routes
server.get('/', (request, response) => {
response.redirect('/home')
})
server.get('*', (request, response) => {
response.render('html/404', {
layout: false
})
})
server.listen(process.env.PORT, () => console.log(`connected to Express on port: ${process.env.PORT}`))