forked from tamaina/vitejs-vite-3gp9bb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.mjs
57 lines (49 loc) · 1.32 KB
/
server.mjs
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
import Fastify from 'fastify';
import fastifyStatic from '@fastify/static';
import fastifyView from '@fastify/view';
import fastifyProxy from '@fastify/http-proxy';
import pug from 'pug';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { readFileSync } from 'node:fs';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
const fastify = Fastify({
logger: true,
});
fastify.register(fastifyView, {
root: join(_dirname, 'views'),
engine: {
pug: pug,
},
defaultContext: {
},
});
if (process.env.NODE_ENV !== 'production') {
fastify.register(fastifyProxy, {
upstream: 'http://127.0.0.1:5173',
prefix: '/assets',
rewritePrefix: '/assets',
});
fastify.get('*', async (request, reply) => {
return reply.view('index', {
clientEntry: {
file: 'src/main.ts',
},
});
});
} else {
fastify.register(fastifyStatic, {
root: join(_dirname, 'built'),
prefix: '/assets/',
decorateReply: false,
});
fastify.get('*', async (request, reply) => {
const clientManifest = JSON.parse(readFileSync('./built/manifest.json', 'utf-8'));
const clientEntry = clientManifest['src/main.ts'];
return reply.view('index', {
clientEntry,
});
});
}
fastify.listen({ port: 3000, host: '0.0.0.0' });