-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev-server.js
64 lines (52 loc) · 1.47 KB
/
dev-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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let express = require("express");
let common = require("./server-common");
let root = process.cwd();
let path = require("path");
async function createServer() {
let app = express();
let vite = await require("vite").createServer({
root,
server: { middlewareMode: true },
appType: 'custom'
});
// Middleware takes care of assets and public folder
app.use(vite.middlewares);
app.use("/", async (req, res) => {
try {
const url = {
path: req.path,
url: req.originalUrl,
query: req.query,
};
// Check for redirect first
let result = common.handleRedirect(req.path);
if (!result) {
let render = await vite
.ssrLoadModule("src/entry.server.tsx")
.then((m) => m.render);
result = await common.handleReact(
render,
url,
path.resolve(__dirname, "./index.html"),
async (template) => {
return await vite.transformIndexHtml(url.url, template);
}
);
}
for (const [key, value] of Object.entries(result.headers)) {
res.setHeader(key, value);
}
return res.status(result.status).end(result.body);
} catch (error) {
vite.ssrFixStacktrace(error);
console.log(error.stack);
res.status(500).end(error.stack);
}
});
return app;
}
createServer().then((app) => {
app.listen(3000, () => {
console.log("HTTP server is running at http://localhost:3000");
});
});