build: reenable eslint type checking
[poolifier.git] / examples / typescript / http-server-pool / express-worker_threads / src / main.ts
CommitLineData
4a6421b5 1import { exit } from 'node:process'
ded253e2 2
fac9ce96 3import express, { type Express, type Request, type Response } from 'express'
ded253e2 4
fac9ce96
JB
5import { requestHandlerPool } from './pool.js'
6
7/**
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.
10 *
11 * The express server is still single-threaded, but the request handler pool is multi-threaded.
12 */
13
14const port = 8080
15const expressApp: Express = express()
16
17const emptyFunction = (): void => {
18 /* Intentional */
19}
20
21// Parse only JSON requests body
22expressApp.use(express.json())
23
24expressApp.all('/api/echo', (req: Request, res: Response) => {
25 requestHandlerPool
6e5d7052 26 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
fac9ce96 27 .execute({ body: req.body }, 'echo')
041dc05b 28 .then(response => {
fac9ce96
JB
29 return res.send(response.body).end()
30 })
31 .catch(emptyFunction)
32})
33
167c661c
JB
34expressApp.get('/api/factorial/:number', (req: Request, res: Response) => {
35 const { number } = req.params
36 requestHandlerPool
80115618 37 .execute({ body: { number: Number.parseInt(number) } }, 'factorial')
041dc05b 38 .then(response => {
167c661c
JB
39 return res.send(response.body).end()
40 })
41 .catch(emptyFunction)
42})
43
a8706532
JB
44try {
45 expressApp.listen(port, () => {
46 console.info(
6e5d7052 47 `⚡️[express server]: Express server is started at http://localhost:${port.toString()}/`
a8706532
JB
48 )
49 })
50} catch (err) {
51 console.error(err)
4a6421b5 52 exit(1)
a8706532 53}