-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.babel.js
102 lines (83 loc) · 2.51 KB
/
app.babel.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
/* global __dirname */
/* global process */
"use strict";
import fs from "fs";
import https from "https";
import path from "path";
import express from "express";
import exphbs from "express-handlebars";
import handlebars from "handlebars";
import bodyParser from "body-parser";
import compression from "compression";
import session from "express-session";
// custom helpers
import { requireHttps } from "./server/helpers/routing.js";
import { defaultPathConfig } from "./server/helpers/pathConfig";
// umbraco imports
import Api from "api";
const API_URL = `${process.env.API_URL}`;
// configuration
const config = {
environment: process.env.NODE_ENV || "development",
isHttps: process.env.isHttps === true || false
};
const app = express();
app.use(compression());
const viewsDir = "./templates";
// setup express to use handlebars as the templating engine
const hbs = exphbs.create({
defaultLayout: "main",
layoutsDir: path.join(__dirname, `${viewsDir}/layouts`),
partialsDir: path.join(__dirname, `${viewsDir}/partials`),
extname: ".hbs"
});
// allows partials to be organised in subfolders
hbs
.getTemplates(path.join(__dirname, `${viewsDir}/partials`))
.then(function(partials) {
for (let partial in partials) {
handlebars.registerPartial(partial, "{{" + partial + "}}");
}
})
.catch(error => {
console.log(`Unable to retrieve templates. Error: ${error}`);
});
app.set("views", path.join(__dirname, `${viewsDir}`));
app.engine("hbs", hbs.engine);
app.set("view engine", "hbs");
// setup server for static assets
app.use(
"/",
express.static(path.join(__dirname, "dist"), { maxAge: 604800000 })
);
// require HTTPS
app.use(requireHttps);
// Setup body parser for parsing POST request bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const sessionExpiration = 20 * 60 * 1000; // 20 minutes
// setup a single server-side URL (disclaimer: no SSR)
app.get("/*", (req, res) => {
// render the response
res.render("index", defaultPathConfig);
});
app.use(function(error, req, res, next) {
console.error(error.message);
if (config.environment === "development") {
throw error;
} else {
res.status(500);
res.render("500", { layout: false });
}
return;
});
// use the environment's port or a random port
const port =
process.env.port ||
(process.env.isDev
? 3000
: Math.floor(Math.random() * (65535 - 1024)) + 1024);
app.listen(port, () => {
console.log(`Running ${config.environment} on localhost:${port}`);
});
module.exports = app;