-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.mjs
40 lines (33 loc) · 937 Bytes
/
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
import Fastify from 'fastify';
import fastifyStatic from '@fastify/static';
import fastifyView from '@fastify/view';
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: {
},
});
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' });