-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (86 loc) · 2.67 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
95
96
97
98
99
100
101
102
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const exphbs = require("express-handlebars");
const bodyParser = require("body-parser");
const session = require("express-session");
const fileUpload = require("express-fileupload");
const routes = require("./controllers/routes");
const MongoStore = require("connect-mongo")(session);
// Loads the enviorenment variables
require("dotenv").config({ path: "./config/keys.env" });
// express-handlebars configuration
const hbs = exphbs.create({
defaultView: "default",
layoutsDir: __dirname + "/views/layouts/",
partialsDir: __dirname + "/views/partials/",
// Custom helper functions
helpers: {
eachFrom: (context, count, options) => {
var ret = "";
context.slice(Math.max(context.length - count, 0)).forEach((elem) => {
ret += options.fn(elem);
});
return ret;
// const slicing = function (context, count) {
// if (context == null) return void 0;
// if (count == null) return context[context.length - 1];
// return context.slice(Math.max(context.length - count, 0));
// };
},
},
});
// Handlebars
app.set("view engine", "handlebars");
app.engine("handlebars", hbs.engine);
// Uploading static files
app.use(express.static("public"));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
/*
This is to allow specific forms and/or links that were submitted/pressed
to send PUT and DELETE request respectively!!!!!!!
*/
app.use((req, res, next) => {
if (req.query.method == "PUT") {
req.method = "PUT";
} else if (req.query.method == "DELETE") {
req.method = "DELETE";
}
next();
});
app.use(fileUpload());
// Session
app.use(
session({
secret: `${process.env.SECRET_KEY}`,
resave: false,
saveUninitialized: true,
// cookie: { secure: true },
cookie: { maxAge: 180 * 60 * 1000 },
store: new MongoStore({ mongooseConnection: mongoose.connection }),
})
);
app.use((req, res, next) => {
res.locals.user = req.session.userInfo;
res.locals.session = req.session;
next();
});
// map the controller to the app object
app.use("/", routes);
// !MONGODB;
mongoose
.connect(process.env.ATLAS_CONNECT, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Connected to MongoDBAtlas ");
})
.catch((err) => console.log(`Error occured as connecting to ${err}`));
//This creates an Express Web Server that listens to HTTP Reuqest on port 3000
app.listen(process.env.PORT || 3000, () => {
console.log("Express server listening on port 3000");
});