-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
86 lines (77 loc) · 2.67 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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import fastify from 'fastify';
import fastifyStatic from '@fastify/static';
import fastifyCompress from '@fastify/compress';
import fastifyMiddie from "@fastify/middie";
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
const PORT = process.env.PORT || 5001;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const absolutePath = (p) => path.resolve(__dirname, p);
const isDev = process.env.NODE_ENV === 'development';
export async function createServer(root = process.cwd(), hmrPort) {
const indexProd = isDev ? '' : fs.readFileSync(absolutePath('dist/client/index.html'), 'utf-8')
const server = fastify({logger: true});
let vite;
if (isDev) {
vite = await (await import('vite')).createServer({
root,
logLevel: 'info',
server: {
middlewareMode: true,
watch: {
usePolling: true,
interval: 100,
},
hmr: {
port: hmrPort,
},
},
appType: 'custom',
});
await server.register(fastifyMiddie, {})
server.use(vite.middlewares);
} else {
await server.register(fastifyCompress);
await server.register(fastifyStatic, {
root: absolutePath('dist/client'),
prefix: '/'
});
}
server.all(isDev ? '*' : '/:path*', async (request, reply) => {
const url = request.raw.url;
const template = isDev
? (await vite.transformIndexHtml(url, fs.readFileSync(absolutePath('index.html'), 'utf-8')))
: indexProd;
const render = isDev
? (await vite.ssrLoadModule('/src/entry-server.jsx')).render
: (await import('./dist/server/entry-server.js')).render;
const context = {};
const appHtml = render(url, context);
if (context.url) return reply.redirect(301, context.url);
const html = template.replace(`<!--ssr-outlet-->`, appHtml);
return reply.status(200).header('Content-Type', 'text/html').send(html);
});
server.setErrorHandler((error, request, reply) => {
console.log(error)
reply.send(
{
statusCode: 500,
error: 'Internal Server Error',
message: error.stack
}
)
})
return {server, vite, port: PORT};
}
createServer().then(({server, vite, port}) => {
server.listen({
port,
}, (err, address) => {
if (err) {
server.log.error(err)
process.exit(1)
}
server.log.info(`Server listening on ${address}`)
})
})