f1271c6e13566ae1b80067a178579b9a3190cb6e
1 import { dirname
, extname
, join
} from
'node:path'
2 import { fileURLToPath
} from
'node:url'
3 import Fastify from
'fastify'
4 import { fastifyPoolifier
} from
'./fastify-poolifier.js'
7 * The fastify server is still a single-threaded application, but the request handling can be multi-threaded.
11 const fastify
= Fastify({
15 const workerFile
= join(
16 dirname(fileURLToPath(import.meta
.url
)),
17 `worker${extname(fileURLToPath(import.meta.url))}`
20 await fastify
.register(fastifyPoolifier
, {
22 enableTasksQueue
: true,
26 errorHandler
: (e
: Error) => {
27 fastify
.log
.error('Thread worker error:', e
)
31 fastify
.all('/api/echo', async (request
) => {
32 return (await fastify
.execute({ body
: request
.body
}, 'echo')).body
36 Params
: { number: number }
37 }>('/api/factorial/:number', async (request
) => {
38 const { number } = request
.params
39 return (await fastify
.execute({ body
: { number } }, 'factorial')).body
43 await fastify
.listen({ port
})
45 fastify
.log
.error(err
)