a71c0b76edf6530a44b9c4d535888a1e5c6fcb3a
1 import { dirname
, extname
, join
} from
'node:path'
2 import { fileURLToPath
} from
'node:url'
3 import Fastify from
'fastify'
4 import { availableParallelism
} from
'poolifier'
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
, {
24 maxWorkers
: availableParallelism(),
25 enableTasksQueue
: true,
29 errorHandler
: (e
: Error) => {
34 fastify
.all('/api/echo', async (request
, reply
) => {
35 await reply
.send((await fastify
.execute({ body
: request
.body
}, 'echo')).body
)
39 Params
: { number: number }
40 }>('/api/factorial/:number', async (request
, reply
) => {
41 const { number } = request
.params
43 (await fastify
.execute({ body
: { number } }, 'factorial')).body
48 await fastify
.listen({ port
})
50 fastify
.log
.error(err
)