1 import { dirname
, extname
, join
} from
'node:path'
2 import { fileURLToPath
} from
'node:url'
3 import { exit
} from
'node:process'
4 import Fastify from
'fastify'
5 import { fastifyPoolifier
} from
'./fastify-poolifier.js'
8 * The fastify server is still a single-threaded application, but the request handling can be multi-threaded.
12 const fastify
= Fastify({
16 const workerFile
= join(
17 dirname(fileURLToPath(import.meta
.url
)),
18 `worker${extname(fileURLToPath(import.meta.url))}`
21 await fastify
.register(fastifyPoolifier
, {
23 enableTasksQueue
: true,
27 errorHandler
: (e
: Error) => {
28 fastify
.log
.error('Thread worker error:', e
)
32 fastify
.all('/api/echo', async request
=> {
33 return (await fastify
.execute({ body
: request
.body
}, 'echo')).body
37 Params
: { number: number }
38 }>('/api/factorial/:number', async request
=> {
39 const { number } = request
.params
40 return (await fastify
.execute({ body
: { number } }, 'factorial')).body
44 await fastify
.listen({ port
})
46 fastify
.log
.error(err
)