-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (72 loc) · 2.41 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
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
require("dotenv/config");
// ℹ️ Connects to the database
require("./db");
// Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const express = require("express");
// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require("hbs");
const app = express();
// ℹ️ This function is getting exported from the config folder. It runs most middlewares
require("./config")(app);
//requiring the countries.geo.js file
const world = require("./public/js/countries.geo");
// default value for title local
const projectName = "covid-travel-advice";
const capitalized = (string) =>
string[0].toUpperCase() + string.slice(1).toLowerCase();
app.locals.title = `${capitalized(projectName)} created with Ironlauncher`;
// Creating the sessions
const session = require("express-session");
const MongoStore = require("connect-mongo");
app.use(
session({
secret: process.env.SESSION_KEY,
saveUninitialized: false,
resave: false,
cookie: {
maxAge: 24 * 60 * 60 * 1000, // in milliseconds
},
store: MongoStore.create({
mongoUrl:
process.env.MONGODB_URI || "mongodb://localhost/covid-travel-advice",
ttl: 24 * 60 * 60, // 1 day => in seconds
}),
})
);
app.use(function (req, res, next) {
res.set(
"Cache-Control",
"no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0"
);
next();
});
// SAMPLE ROUTE
app.get("/", (req, res, next) => {
let loc = [51.505, -0.09];
let mapCountry = world.features.map((elem, index) => {
return elem.properties.name;
});
// Sending some data to the hbs page
//Always stringify data that the scripts might use in your hbs file
res.render("index.hbs", {
loc: JSON.stringify(loc),
layout: false,
mapCountry,
});
});
// 👇 Start handling routes here
const authRoutes = require("./routes/auth.routes");
app.use("/", authRoutes);
const infoRoutes = require("./routes/covid-info.routes");
app.use("/", infoRoutes);
const adminRoutes = require("./routes/user-entry-verify.routes");
app.use("/", adminRoutes);
const profileRoutes = require("./routes/profile.routes");
app.use("/", profileRoutes);
// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require("./error-handling")(app);
module.exports = app;