-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (57 loc) · 1.9 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
// Require dependencies
const path = require("path");
const dotenv = require("dotenv").config();
const bodyParser = require("body-parser");
const methodOverride = require("method-override");
const passport = require("passport");
const localStrategy = require("passport-local");
const passportLocalMongoose = require("passport-local-mongoose");
const mongoose = require("mongoose");
const express = require("express");
const app = express();
// General setup
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride("_method"));
// MongoDB setup
mongoose.connect(process.env.MLAB_URI, {useNewUrlParser: true});
// Schemas
const Staff = require("./models/staff");
const Patient = require("./models/patient");
const Log = require("./models/log");
// Passport configuration
app.use(require("express-session")({
secret : "BioEng18 WebDev",
resave : false,
saveUninitialized : false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(Staff.authenticate()));
passport.serializeUser(Staff.serializeUser());
passport.deserializeUser(Staff.deserializeUser());
// Middleware so that req.user will be available in every template
// This needs to be put before routes
app.use((req, res, next) => {
res.locals.currentUser = req.user;
next();
});
// Require routes
const baseRoutes = require("./routes/base");
const authRoutes = require("./routes/auth");
const patientRoutes = require("./routes/patients");
const logRoutes = require("./routes/logs");
// Routes
app.use(baseRoutes);
app.use(authRoutes);
app.use("/patients", patientRoutes);
app.use("/patients/:patient_id/logs", logRoutes);
app.get("*", (req, res) => {
res.redirect("/");
});
// Define port and listen
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});