-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (40 loc) · 1.46 KB
/
server.js
File metadata and controls
45 lines (40 loc) · 1.46 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
/* eslint-disable no-console */
import compression from 'compression';
import express from 'express';
import morgan from 'morgan';
// Short-circuit the type-checking of the built output.
const BUILD_PATH = './build/server/index.js';
const DEVELOPMENT = process.env.NODE_ENV === 'development';
const PORT = Number.parseInt(process.env.PORT || '3000');
const app = express();
app.use(compression());
app.disable('x-powered-by');
if (DEVELOPMENT) {
console.log('Starting development server');
const viteDevServer = await import('vite').then((vite) =>
vite.createServer({
server: { middlewareMode: true },
})
);
app.use(viteDevServer.middlewares);
app.use(async (req, res, next) => {
try {
const source = await viteDevServer.ssrLoadModule('./server/app.ts');
return await source.app(req, res, next);
} catch (error) {
if (typeof error === 'object' && error instanceof Error) {
viteDevServer.ssrFixStacktrace(error);
}
next(error);
}
});
} else {
console.log('Starting production server');
app.use('/assets', express.static('build/client/assets', { immutable: true, maxAge: '1y' }));
app.use(morgan('tiny'));
app.use(express.static('build/client', { maxAge: '1h' }));
app.use(await import(BUILD_PATH).then((mod) => mod.app));
}
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});