-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (56 loc) · 1.66 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
"use strict";
const express = require("express");
const app = express();
const passport = require("passport");
var session = require("express-session");
let bodyParser = require("body-parser");
const flash = require("express-flash");
require("dotenv").config();
const port = process.env.PORT || 8080;
app.set("models", require("./models"));
// reminder: in controllers, use const { Computer } = req.app.get('models');
app.set("view engine", "pug");
app.use('/public', express.static(__dirname + "/public"));
let routes = require("./routes/");
// Begin middleware stack
// Inject session persistence into middleware stack
app.use(
session({
secret: "keyboard cat",
resave: true,
saveUninitialized: true
})
); // session secret
//execute passport strategies file
require("./config/passport-strat.js");
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use((req, res, next) => {
res.locals.session = req.session;
// console.log('res.locals.session', res.locals.session);
next();
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(flash());
// note that this needs to be after the above stuff
app.use(routes);
// 404 error handler
app.use((req, res, next) => {
let error = new Error("sorry, not found.");
error.status = 404;
next(error);
});
// Add error handler to pipe all server errors to from the routing middleware
app.use((err, req, res, next) => {
console.log(err);
res.status(err.status || 500);
res.json({
message: "A problem occurred.",
err: err
});
});
module.exports = app;
app.listen(port, () => {
console.log(`listening on port ${port}`);
});