-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (56 loc) · 1.89 KB
/
Copy pathserver.js
File metadata and controls
63 lines (56 loc) · 1.89 KB
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
import { createRsbuild, loadConfig } from '@rsbuild/core';
import { createRequestHandler } from '@react-router/express';
import {
loadReactRouterServerBuild,
resolveReactRouterServerBuild,
} from 'rsbuild-plugin-react-router';
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const isDev = process.env.NODE_ENV !== 'production';
async function startServer() {
/** @type {import('@rsbuild/core').RsbuildDevServer | undefined} */
let devServer;
/** @type {import('react-router').ServerBuild | (() => Promise<import('react-router').ServerBuild>)} */
let build;
if (isDev) {
const config = await loadConfig();
const rsbuild = await createRsbuild({
rsbuildConfig: config.content,
});
const currentDevServer = await rsbuild.createDevServer();
devServer = currentDevServer;
app.use(currentDevServer.middlewares);
build = () => loadReactRouterServerBuild(currentDevServer);
} else {
app.use(
express.static(path.join(__dirname, 'build/client'), {
index: false,
})
);
const productionBuildPath = './build/server/static/js/app.js';
build = await resolveReactRouterServerBuild(import(productionBuildPath));
}
app.use(
createRequestHandler({
build,
mode: isDev ? 'development' : 'production',
getLoadContext() {
return {
VALUE_FROM_EXPRESS: 'Hello from Express',
};
},
})
);
const port = Number.parseInt(process.env.PORT || '3000', 10);
const server = app.listen(port, () => {
const mode = isDev ? 'Development' : 'Production';
console.log(`${mode} server is running on http://localhost:${port}`);
devServer?.afterListen();
});
devServer?.connectWebSocket({ server });
}
startServer().catch(console.error);