-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
94 lines (80 loc) · 2.84 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
const express = require("express");
const mongoose = require("mongoose");
const flash = require("connect-flash");
const session = require("express-session");
const passport = require('passport');
const { Store } = require("express-session");
const MongoStore = require("connect-mongo"); // for storing session
const database = require("./config/db");
const dotenv = require("dotenv"); // Enviroment Variables Global
const exphbs = require('express-handlebars'); // view Engine
const path = require("path"); // Node
const methodOverride = require('method-override') // for overriding PUT and DELETE , since forms can only have GET | POST
//config accessing the global variables
dotenv.config({ path: "./config/config.env" });
//passport config
//passing the passport variable in the specified directory
require('./config/passport')(passport);
database();
const app = express();
//Body Parser
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
//method override init
app.use(methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
let method = req.body._method
delete req.body._method
return method
}
}));
const { saveData, formatDate, select } = require("./helpers/hbs");
//view engine handlebars
app.engine('.hbs', exphbs({ helpers: { saveData, formatDate, select }, defaultLayout: 'main', extname: '.hbs' }));
app.set('view engine', '.hbs');
// Express session
app.use(
session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true,
// store: MongoStore.create({ mongoUrl: process.env.MONGO_URI }) //storing the session in D
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
//the static public folder carries img or css or other static files
app.use(express.static(path.join(__dirname, "public")));
//connect flash
app.use(flash());
//For Login page we declare global variables
app.use((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();
});
app.use(function (req, res, next) {
if (req.user) {
console.log("USER");
console.log(req.user);
}
if (req.body) {
console.log("BODY");
console.log(req.body);
}
if (req.params) {
console.log("PARAMS");
console.log(req.params);
}
next();
});
app.use("/", require("./routes/index"));
app.use("/user", require("./routes/users"));
app.use("/auth", require("./routes/auth"));
app.use("/api/notify", require("./routes/notify"));
app.use("/api/history", require("./routes/history"));
const PORT = process.env.PORT || 5000;
app.listen(PORT, console.log(`Server is running on port ${PORT}`));