-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (105 loc) · 2.73 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const path = require("path");
const cors = require("cors");
const logger = require("morgan");
const express = require("express");
const passport = require("passport");
const bodyParser = require("body-parser");
const session = require("express-session");
const createError = require("http-errors");
const cookieParser = require("cookie-parser");
const LocalStrategy = require("passport-local").Strategy;
const Account = require("./models/account");
const index = require("./routes/index");
const redis = require("redis");
const app = express();
app.use(logger("dev"));
// app.use("/static", express.static(path.join(__dirname, "public", "static")));
/**
* Redis setup for session storage
*/
let RedisStore = require("connect-redis")(session);
let client = redis.createClient({
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASSWORD,
port: process.env.REDIS_PORT
});
/**
* Only user application/json body parser... no form encoding
*/
app.use(bodyParser.json());
/**
* User cookie parser because session is in cookie
*/
app.use(cookieParser());
/**
* Sessions are in cookie, only give a domain if production
*/
if (process.env.NODE_ENV === "production") {
app.use(
session({
store: new RedisStore({ client }),
secret: process.env.SECRET,
saveUninitialized: true,
resave: false,
cookie: {
domain: ".ourlabels.org"
}
})
);
} else {
app.use(
session({
secret: process.env.SECRET,
saveUninitialized: true,
resave: false
})
);
}
/**
* Cors configuration
*/
var whitelist = /^https?:\/\/([a-z]+.)?ourlabels.org$/i;
var corsOptions = {
origin: function(origin, callback) {
if (process.env.NODE_ENV !== "production") {
callback(null, true);
} else {
if (whitelist.test(origin)) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
}
},
credentials: true
};
app.use(cors(corsOptions));
/**
* Passport configuration... once registered will be found by routes
*/
passport.use(
"local",
new LocalStrategy({ passReqToCallback: true }, Account.authenticate)
);
passport.serializeUser(Account.serializeUser);
passport.deserializeUser(Account.deserializeUser);
app.use(passport.initialize());
app.use(passport.session());
/**
* Add root routes
*/
app.use("/", index);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;