-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (61 loc) · 2.07 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
const express = require("express");
const mongoose = require("mongoose");
const session = require("express-session");
const bodyParser = require("body-parser");
const flash = require("connect-flash");
const passport = require("passport");
const expressValidator = require("express-validator");
const routes = require("./routes/index");
const errorHandlers = require("./handlers/errorHandlers");
const helpers = require('./helper');
require("./handlers/passport");
//enviornment variables
require("dotenv").config({ path: "./variables.env" });
//create our express app
const app = express();
//loading the public dir in order to use external stylesheets, images, etc
app.use(express.static("public"));
// view engine setup
app.set("views", "./views"); // folder where we keep out views
app.set("view engine", "pug"); // I like the pug engine
// Takes the raw requests and turns them into usable properties on req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
app.use(
session({
secret: process.env.SECRET,
key: process.env.KEY,
resave: false,
saveUninitialized: false
})
);
// // Passport JS for handing logins
app.use(passport.initialize());
app.use(passport.session());
// use connect-flash to display messages
app.use(flash());
//store flashes into locals to be displayed for comfirmation messages or errors.
app.use((req, res, next) => {
res.locals.help = helpers;
res.locals.flashes = req.flash();
res.locals.user = req.user || null;
next();
});
//linking up the database
mongoose.connect(process.env.DATABASE);
mongoose.connection.on("error", err => {
console.error(`🙅 🚫 🙅 🚫 🙅 🚫 🙅 🚫 → ${err.message}`);
});
//import our models
require("./models/Parks");
require("./models/User");
// require("./models/Review");
//handle our routes
app.use("/", routes);
//if the above routes aren't found we mark it as 404 and move along
app.use(errorHandlers.notFound);
app.listen(process.env.PORT || 8000, (req, res) => {
console.log("Seems to be running!");
});
module.exports = app;