-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (58 loc) · 1.75 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
const express = require("express");
require("dotenv").config();
require("./models");
const app = express();
const path = require("path");
const Handlebars = require("handlebars");
const expressHandlebars = require("express-handlebars");
const {
allowInsecurePrototypeAccess
} = require("@handlebars/allow-prototype-access");
const bodyparser = require("body-parser");
const cors = require("cors");
const indexRoutes = require("./routes/index");
const adminRoutes = require("./routes/admin");
const databaseRoutes = require("./routes/database");
const morgan = require("morgan");
const errorHandler = require("errorhandler");
app.use(morgan("dev"));
app.use(
bodyparser.urlencoded({
extended: true
})
);
app.use(bodyparser.json());
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method == "OPTIONS") {
res.header("Access-Control-Allow-Methods", "GET, POST");
return res.status(200).json({});
}
next();
});
if ("development" === app.get("env")) {
app.use(errorHandler());
}
app.set("views", path.join(__dirname, "views"));
app.engine(
"hbs",
expressHandlebars({
extname: "hbs",
defaultLayout: "mainLayout",
layoutsDir: __dirname + "/views/layouts",
handlebars: allowInsecurePrototypeAccess(Handlebars)
})
);
app.set("view engine", "hbs");
app.use(express.static(`${__dirname}/public/`));
app.use("/", indexRoutes);
app.use("/admin", adminRoutes);
app.use("/database", databaseRoutes);
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server started, listening on ${PORT}`);
});