-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
73 lines (68 loc) · 2.49 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
import { createServer } from "http";
import React from "react";
import ReactDOMServer from "react-dom/server";
import { StaticRouter } from "react-router";
import App from "./src/App";
import buildPath from './build/asset-manifest.json';
import fs from 'fs';
import url from 'url';
import path from 'path';
import { getType as getMimeType} from "mime";
createServer((req, res) => {
if (req.url.startsWith('/static/')) {
let pathname = "./build" + url.parse(req.url).pathname;
fs.exists(pathname, function (exists) {
if (!exists) {
res.writeHead("404", { "content-type": "text/plain" });
res.write("404,not found!");
res.end();
} else {
fs.readFile(pathname, function (err, data) {
if (err) {
res.writeHead(500, { "content-type": "text/plain" });
res.end(err);
} else {
let ext = path.extname(pathname);
let contenttype = getMimeType(ext) || "text/plain";
res.writeHead(200, { "content-type": contenttype });
res.write(data);
res.end();
}
});
}
});
return;
}
const context = {};
const frontComponents = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
);
if (context.url) {
res.writeHead(301, {
Location: context.url
});
res.end();
} else {
const frontHtml = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<title>StartRoute</title>
<link rel="stylesheet" type="text/css" href="${buildPath.files["main.css"]}">
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root">${frontComponents}</div>
<script src="${buildPath.files['main.js']}"></script>
</body>
</html>`
res.write(frontHtml);
res.end();
}
}).listen(3000);