1 import express
, { type Express
, type Request
, type Response
} from
'express'
2 import { requestHandlerPool
} from
'./pool.js'
5 * 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.
6 * The request handler pool can also be used as a middleware in the express stack: application or router level.
8 * The express server is still single-threaded, but the request handler pool is multi-threaded.
12 const expressApp
: Express
= express()
14 const emptyFunction
= (): void => {
18 // Parse only JSON requests body
19 expressApp
.use(express
.json())
21 expressApp
.all('/api/echo', (req
: Request
, res
: Response
) => {
23 .execute({ body
: req
.body
}, 'echo')
25 return res
.send(response
.body
).end()
30 expressApp
.get('/api/factorial/:number', (req
: Request
, res
: Response
) => {
31 const { number } = req
.params
33 .execute({ body
: { number: parseInt(number) } }, 'factorial')
35 return res
.send(response
.body
).end()
41 expressApp
.listen(port
, () => {
43 `⚡️[express server]: Express server is started at http://localhost:${port}/`