-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
38 lines (31 loc) · 919 Bytes
/
server.ts
File metadata and controls
38 lines (31 loc) · 919 Bytes
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
import express from "express";
import React from "react";
import ReactDOMServer from "react-dom/server";
import { App } from "./src/index.js";
const app = express();
app.get("*", (req: express.Request, res: express.Response, next) => {
if (req.url === "/dist/bundle.js" || req.url === "/favicon.ico") {
return next();
}
const currentPath = req.url === "/" ? "/" : req.url;
console.log("Current Path from Server:", currentPath);
const appHtml = ReactDOMServer.renderToString(
React.createElement(App, { currentPath })
);
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>React Framework</title>
</head>
<body>
<div id="root">${appHtml}</div>
<script src="/dist/bundle.js"></script>
</body>
</html>
`);
});
app.use(express.static("public"));
app.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});