-
Notifications
You must be signed in to change notification settings - Fork 263
/
app.js
90 lines (72 loc) · 2.78 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
const express = require("express");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const path = require("path");
const bodyParser = require("body-parser");
const promisify = require("es6-promisify");
const apiRouter = require("./routes/api");
const authApiRouter = require("./routes/authApi");
const errorHandlers = require("./handlers/errorHandlers");
const { isValidToken } = require("./controllers/authController");
require("dotenv").config({ path: ".variables.env" });
// create our Express app
const app = express();
// serves up static files from the public folder. Anything in public/ will just be served up as the file it is
app.use(express.static(path.join(__dirname, "public")));
// Takes the raw requests and turns them into usable properties on req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Sessions allow us to Contact data on visitors from request to request
// This keeps admins logged in and allows us to send flash messages
app.use(
session({
secret: process.env.SECRET,
key: process.env.KEY,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.DATABASE }),
})
);
// pass variables to our templates + all requests
app.use((req, res, next) => {
res.locals.admin = req.admin || null;
res.locals.currentPath = req.path;
next();
});
// promisify some callback based APIs
app.use((req, res, next) => {
req.login = promisify(req.login, req);
next();
});
// Here our API Routes
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Allow-Methods", "GET,PATCH,PUT,POST,DELETE");
res.header("Access-Control-Expose-Headers", "Content-Length");
res.header(
"Access-Control-Allow-Headers",
"Accept, Authorization,x-auth-token, Content-Type, X-Requested-With, Range"
);
if (req.method === "OPTIONS") {
return res.sendStatus(200);
} else {
return next();
}
});
app.use("/api", authApiRouter);
// for development & production don't use this line app.use("/api", apiRouter); , this is just demo login contoller
app.use("/api", apiRouter);
//uncomment line below // app.use("/api", isValidToken, apiRouter);
// app.use("/api", isValidToken, apiRouter);
// If that above routes didnt work, we 404 them and forward to error handler
app.use(errorHandlers.notFound);
// Otherwise this was a really bad error we didn't expect! Shoot eh
if (app.get("env") === "development") {
/* Development Error Handler - Prints stack trace */
app.use(errorHandlers.developmentErrors);
}
// production error handler
app.use(errorHandlers.productionErrors);
// done! we export it so we can start the site in start.js
module.exports = app;