forked from render-examples/hapi-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (51 loc) · 1.37 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
"use strict";
const Hapi = require("@hapi/hapi");
const server = Hapi.server({
port: 3000,
host: "0.0.0.0" // needed for Render deployment
});
server.route({
method: "GET",
path: "/{path*}",
handler: (request, h) => {
let redirect_url = '';
if (request.info.hostname === process.env.AUTH_HOSTNAME) {
redirect_url = process.env.REDIRECT_TO_AUTH + '/' + request.params.path;
} else if (request.info.hostname === process.env.APP_HOSTNAME) {
redirect_url = process.env.REDIRECT_TO_APP + '/' + request.params.path;
} else if (request.info.hostname === process.env.LEGACY_HOSTNAME) {
redirect_url = process.env.REDIRECT_TO_SCHOOLS;
} else {
redirect_url = process.env.REDIRECT_TO_APP;
}
return h.redirect(redirect_url).code(301);
}
});
const init = async () => {
await server.register([
{
plugin: require("@hapi/inert"),
options: {}
},
{
plugin: require("hapi-pino"),
options: {
prettyPrint: true,
logEvents: ["response", "onPostStart"]
}
}]);
server.route({
method: "GET",
path: "/hello",
handler: (request, h) => {
return h.file("./public/hello.html");
}
});
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
process.on("unhandledRejection", (err) => {
console.log(err);
process.exit(1);
});
init();