-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (65 loc) · 2.02 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
const createError = require("http-errors");
require("dotenv").config();
const path = require("path");
const logger = require("morgan");
const express = require("express");
const mongoose = require("mongoose");
const passport = require("passport");
const flash = require("connect-flash");
const session = require("express-session");
const bodyParser = require("body-parser");
// Passport Config
require("./config/passport");
require("./config/passport-google");
let MONGODB_URI = `mongodb+srv://yadda:${process.env.ATLAS_PASSWORD}@yadda-lxawk.mongodb.net/test?retryWrites=true&w=majority`;
mongoose
.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(function () {
console.log("mongoose connection open");
})
.catch(function (err) {
console.error(err);
});
const app = express();
app.locals.pretty = app.get("env") === "development"; // pretty print html
// view engine setup pug and static
app.use(express.static(path.join(__dirname, "public")));
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
// Express body parser
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(logger("dev"));
// Express session
app.use(
require("express-session")({
// passport initialize
secret: "ioeruir!jkæljdklasjdæa&%", // do the keyboard cat
resave: true, // to create entropy
saveUninitialized: false,
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Connect flash
app.use(flash());
// Global variables
app.use(function (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();
});
// Routes
app.use("/user", require("./routes/user.js"));
app.use("/", require("./routes/index.js"));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
res.status("404").send({ message: "invalid request" });
});
module.exports = app;