-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (26 loc) · 783 Bytes
/
index.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
// hack
if (typeof window === 'undefined') {
global.window = {};
}
const express = require('express');
const fs = require('fs');
const path = require('path');
const { renderToString } = require('react-dom/server');
const SSR = require('../dist/search-server.js');
const template = fs.readFileSync(path.join(__dirname, '../dist/search.html'), 'utf-8');
const server = (port) => {
const app = express();
app.use(express.static('dist'));
app.get('/search', (req, res) => {
const html = renderMarKup(renderToString(SSR));
res.status(200).send(html);
});
app.listen(port, () => {
console.log('server is runing ok');
});
};
server(process.env.PORT || 3000);
// 模板
const renderMarKup = (str) => {
return template.replace('<!--HTML_DOEM-->', str);
};