This repository was archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.tsx
More file actions
45 lines (40 loc) · 1.22 KB
/
server.tsx
File metadata and controls
45 lines (40 loc) · 1.22 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
import path from 'path';
import fs from 'fs';
import React from 'react';
import express from 'express';
import { StaticRouter } from 'react-router-dom';
import ReactDOMServer from 'react-dom/server';
import Routes from 'Routes';
import './src/polyfills';
import App from './src/App';
const PORT = process.env.PORT || 3006;
const app = express();
fs.readFile(path.resolve('./dist/index.html'), 'utf8', (err, data) => {
if (err) {
console.error('Something went wrong:', err);
return;
} else {
const renderHtml = (url: string): string => {
const context = {};
//Create the rendered HTML for this route
const app = ReactDOMServer.renderToString(
<StaticRouter context={context} location={url}>
<App />
</StaticRouter>,
);
return data.replace('<div id="root"></div>', `<div id="root">${app}</div>`);
};
//For every Route that we've registered render the HTML
//and create a listener that serves it
Object.values(Routes).forEach(({ path: urlPath }) => {
const renderedHtml = renderHtml(urlPath);
app.get(urlPath, (_req, res) => {
res.send(renderedHtml);
});
});
app.use(express.static('./dist'));
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
}
});