-
-
Notifications
You must be signed in to change notification settings - Fork 349
/
index.js
36 lines (30 loc) · 1 KB
/
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
35
36
const express = require("express");
const { createPageRender } = require("vite-plugin-ssr");
const vite = require("vite");
const isProduction = process.env.NODE_ENV === "production";
const root = `${__dirname}/..`;
startServer();
async function startServer() {
const app = express();
let viteDevServer;
if (isProduction) {
app.use(express.static(`${root}/dist/client`, { index: false }));
} else {
viteDevServer = await vite.createServer({
root,
server: { middlewareMode: true },
});
app.use(viteDevServer.middlewares);
}
const renderPage = createPageRender({ viteDevServer, isProduction, root });
app.get("*", async (req, res, next) => {
const url = req.originalUrl;
const contextProps = {};
const result = await renderPage({ url, contextProps });
if (result.nothingRendered) return next();
res.status(result.statusCode).send(result.renderResult);
});
const port = 3000;
app.listen(port);
console.log(`Server running at http://localhost:${port}`);
}