-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (32 loc) · 1.09 KB
/
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
37
38
39
import express from "express";
import React from "react";
import { renderToString } from "react-dom/server";
import fs from "fs";
import path from "path";
// React 컴포넌트 정의 (JSX 없이)
const App = () => {
return React.createElement(
"div",
null,
React.createElement("h1", null, "Hello, SSR with React!"),
React.createElement("p", null, "This content is rendered on the server.")
);
};
// Express 서버 설정
const app = express();
app.use("/static", express.static("public"));
app.get("/", (req, res) => {
const appString = renderToString(React.createElement(App));
const indexFile = path.resolve("./public/index.html");
fs.readFile(indexFile, "utf8", (err, data) => {
if (err) {
console.error("Error reading index.html file:", err);
return res.status(500).send("An error occurred");
}
res.setHeader("Cache-Control", "no-store"); // 캐시 비활성화
res.status(200).send(data.replace('<div id="root"></div>', `<div id="root">${appString}</div>`));
});
});
app.listen(3000, () => {
console.log("Server is listening on port 3000");
});