forked from microfrontends/products-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
28 lines (22 loc) · 805 Bytes
/
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
const path = require('path');
const fs = require('fs');
const express = require('express');
const React = require('react');
const App = require('./transpiled/App.js').default;
const { renderToString } = require('react-dom/server');
const server = express();
server.get('/', (req, res) => {
const htmlPath = path.resolve(__dirname, 'build', 'index.html');
fs.readFile(htmlPath, 'utf8', (err, html) => {
const rootElem = '<div id="products-list-root">';
const renderedApp = renderToString(React.createElement(App, null));
setTimeout(() => {
res.send(html.replace(rootElem, rootElem + renderedApp));
}, 5000);
});
});
server.use(express.static('build'));
const port = process.env.PORT || 8080;
server.listen(port, () => {
console.log(`App listening on port ${port}`);
});