This repository was archived by the owner on Jan 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathserver.js
More file actions
103 lines (92 loc) · 2.36 KB
/
Copy pathserver.js
File metadata and controls
103 lines (92 loc) · 2.36 KB
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
import {Server} from "hapi";
import React from "react";
import Router from "react-router";
import Transmit from "react-transmit";
import routes from "views/Routes";
import url from "url";
var hostname = process.env.HOSTNAME || "localhost";
/**
* Start Hapi server on port 8000.
*/
const server = new Server();
server.connection({host: hostname, port: process.env.PORT || 8000});
server.start(function () {
console.info("==> ✅ Server is listening");
console.info("==> 🌎 Go to " + server.info.uri.toLowerCase());
});
/**
* Attempt to serve static requests from the public folder.
*/
server.route({
method: "*",
path: "/{params*}",
handler: (request, reply) => {
reply.file("static" + request.path);
}
});
/**
* Endpoint that proxies all GitHub API requests to https://api.github.com.
*/
server.route({
method: "*",
path: "/api/github/{path*}",
handler: {
proxy: {
passThrough: true,
mapUri (request, callback) {
callback(null, url.format({
protocol: "https",
host: "api.github.com",
pathname: request.params.path,
query: request.query
}));
}
}
}
});
server.route({
method: "GET",
path: "/api/names/{nameId}",
handler: (request, reply) => {
const exampleNames = {
"1": "John",
"2": "Jack",
"3": "Jane"
};
const name = exampleNames[request.params.nameId];
const result = {
name: name ? name : "Name not found"
};
reply(result);
}
});
/**
* Catch dynamic requests here to fire-up React Router.
*/
server.ext("onPreResponse", (request, reply) => {
if (typeof request.response.statusCode !== "undefined") {
return reply.continue();
}
Router.run(routes, request.path, (Handler, router) => {
Transmit.renderToString(Handler, {queryParams: router.params}).then(({reactString, reactData}) => {
let output = (
`<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>react-isomorphic-starterkit</title>
<link rel="shortcut icon" href="/favicon.ico">
</head>
<body>
<div id="react-root">${reactString}</div>
</body>
</html>`
);
const webserver = process.env.NODE_ENV === "production" ? "" : "//" + hostname + ":8080";
output = Transmit.injectIntoMarkup(output, reactData, [`${webserver}/dist/client.js`]);
reply(output);
}).catch((error) => {
reply(error.stack).type("text/plain").code(500);
});
})
});