615143646507d8e2f33922426384a19e8e5e906c
1 import { exit
} from
'node:process'
3 import express
, { type Express
, type Request
, type Response
} from
'express'
5 import { requestHandlerPool
} from
'./pool.js'
8 * 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.
9 * The request handler pool can also be used as a middleware in the express stack: application or router level.
11 * The express server is still single-threaded, but the request handler pool is multi-threaded.
15 const expressApp
: Express
= express()
17 const emptyFunction
= (): void => {
21 // Parse only JSON requests body
22 expressApp
.use(express
.json())
24 expressApp
.all('/api/echo', (req
: Request
, res
: Response
) => {
26 .execute({ body
: req
.body
}, 'echo')
28 return res
.send(response
.body
).end()
33 expressApp
.get('/api/factorial/:number', (req
: Request
, res
: Response
) => {
34 const { number } = req
.params
36 .execute({ body
: { number: Number.parseInt(number) } }, 'factorial')
38 return res
.send(response
.body
).end()
44 expressApp
.listen(port
, () => {
46 `⚡️[express server]: Express server is started at http://localhost:${port.toString()}/`