1 import { dirname
, extname
, join
} from
'node:path'
2 import { exit
} from
'node:process'
3 import { fileURLToPath
} from
'node:url'
5 import Fastify from
'fastify'
7 import { fastifyPoolifier
} from
'./fastify-poolifier.js'
10 * The fastify server is still a single-threaded application, but the request handling can be multi-threaded.
14 const fastify
= Fastify({
18 const workerFile
= join(
19 dirname(fileURLToPath(import.meta
.url
)),
20 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
21 `worker${extname(fileURLToPath(import.meta.url))}`
24 await fastify
.register(fastifyPoolifier
, {
26 enableTasksQueue
: true,
30 errorHandler
: (e
: Error) => {
31 fastify
.log
.error('Thread worker error:', e
)
35 fastify
.all('/api/echo', async request
=> {
36 return (await fastify
.execute({ body
: request
.body
}, 'echo')).body
40 Params
: { number: number }
41 }>('/api/factorial/:number', async request
=> {
42 const { number } = request
.params
43 return (await fastify
.execute({ body
: { number } }, 'factorial')).body
47 await fastify
.listen({ port
})
49 fastify
.log
.error(err
)