1 import { exit
} from
'node:process'
2 import express
, { type Express
, type Request
, type Response
} from
'express'
3 import { requestHandlerPool
} from
'./pool.js'
6 * The goal of this example is to show how to use the poolifier library to create a pool of workers that can handle HTTP requests.
7 * The request handler pool can also be used as a middleware in the express stack: application or router level.
9 * The express server is still single-threaded, but the request handler pool is multi-threaded.
13 const expressApp
: Express
= express()
15 const emptyFunction
= (): void => {
19 // Parse only JSON requests body
20 expressApp
.use(express
.json())
22 expressApp
.all('/api/echo', (req
: Request
, res
: Response
) => {
24 .execute({ body
: req
.body
}, 'echo')
26 return res
.send(response
.body
).end()
31 expressApp
.get('/api/factorial/:number', (req
: Request
, res
: Response
) => {
32 const { number } = req
.params
34 .execute({ body
: { number: parseInt(number) } }, 'factorial')
36 return res
.send(response
.body
).end()
42 expressApp
.listen(port
, () => {
44 `⚡️[express server]: Express server is started at http://localhost:${port}/`