forked from veteransaffairscanada/vac-benefits-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
139 lines (125 loc) · 4.57 KB
/
server.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const next = require("next");
const Cookies = require("universal-cookie");
const helmet = require("helmet");
const { parseUserAgent } = require("detect-browser");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
const i18nextMiddleware = require("i18next-express-middleware");
const Backend = require("i18next-node-fs-backend");
const { i18nInstance } = require("./i18n");
const deploy = require("./utils/deploy_notification");
const airTable = require("./utils/airtable_es2015");
const redux2i18n = require("./utils/redux2i18n");
Promise.resolve(airTable.hydrateFromAirtable()).then(data => {
// init i18next with serverside settings
// using i18next-express-middleware
i18nInstance
.use(Backend)
.use(i18nextMiddleware.LanguageDetector)
.init(
{
wait: true,
fallbackLng: "en",
preload: ["en", "fr"],
ns: ["common"],
backend: {
loadPath: path.join(__dirname, "/locales/{{lng}}/{{ns}}.json"),
addPath: path.join(__dirname, "/locales/{{lng}}/{{ns}}.missing.json")
}
},
() => {
// loaded translations we can bootstrap our routes
app.prepare().then(() => {
const server = express();
server.use(bodyParser.json());
server.use(helmet());
// enable middleware for i18next
server.use(i18nextMiddleware.handle(i18nInstance));
// serve locales for client
server.use(
"/locales",
express.static(path.join(__dirname, "/locales"))
);
// missing keys
server.post(
"/locales/add/:lng/:ns",
i18nextMiddleware.missingKeyHandler(i18nInstance)
);
// submitting Feedback
server.post("/submitComment", (req, res) => {
console.log("Submitting comments");
airTable.writeFeedback(req.body);
res.sendStatus(200);
});
// use next.js
server.get("*", (req, res) => {
// Check if browse is less than IE 11
const ua = req.headers["user-agent"];
const browser = parseUserAgent(ua);
setTimeout(function() {
Promise.resolve(airTable.hydrateFromAirtable()).then(newData => {
data = newData;
});
}, 1000 * 60 * 60);
req.data = data;
if (
browser &&
browser.name === "ie" &&
parseInt(browser.version) < 11 &&
!req.url.includes("all-benefits")
) {
res.sendFile("fallback-pages/browser-incompatible.html", {
root: __dirname
});
} else if (req.url.includes("refresh")) {
console.log("Refreshing Cache ...");
let referrer = req.header("Referer") || "/";
Promise.resolve(airTable.hydrateFromAirtable()).then(newData => {
data = newData;
res.redirect(referrer);
console.log("Cache refreshed @ " + data.timestamp);
});
} else {
req.data.favouriteBenefits = new Cookies(req.headers.cookie).get(
"favouriteBenefits"
);
handle(req, res);
}
});
const port = process.env.PORT || 3000;
server.listen(port, err => {
if (err) throw err;
console.log("> Ready on http://localhost:" + port);
deploy.notify();
});
});
}
);
redux2i18n.redux2i18n(i18nInstance, data.translations);
});
// this code should run when the data-validation page is loaded and send that page the results
// note that we can't check the urls in the browser because of CO
//
// var checkLinks = async function checklinks(benefits) {
// var brokenLinks = [];
// var responseEn, responseFr;
// for (let benefit of benefits) {
// responseEn = await fetch(benefit.benefitPageEn);
// responseFr = await fetch(benefit.benefitPageFr);
// if (responseEn.status !== 200) {
// brokenLinks.push(benefit.benefitPageEn);
// }
// if (responseFr.status !== 200) {
// brokenLinks.push(benefit.benefitPageFr);
// }
// console.log("type", typeof responseEn.body);
// if (responseEn.body.indexOf(benefit.benefitPageEn) === -1) {
// console.log(benefit.benefitPageEn, responseEn, "BAD URL");
// }
// }
// return brokenLinks;
// };