-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (29 loc) · 831 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
29
30
31
32
33
34
35
36
const express = require("express");
const { h } = require("preact");
const renderToString = require("preact-render-to-string");
const path = require("path");
const chalk = require("chalk");
const App = require('./client/App');
const app = express();
const port = 8080;
app.use(express.static(path.join(__dirname, "dist")));
app.listen(port);
app.get("*", (req, res) => {
const html = renderToString(<App />);
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Preact SSR</title>
</head>
<body>
<div id="app">${html}</div>
<script src="./app.js"></script>
</body>
</html>
`);
});
console.log(chalk.blue(`Server started at http://localhost:${port}`));