This repo demonstrates the usage of worker threads to separate part of the app in order not to block the rest of the endpoints
My goal was to run separate express.Router in the thread and pass request, response objects, but that's not possible due to the limitations of postMessage data.
The first example threads
shows offloading the work to the worker.
The second example threads
app takes a different approach. It uses worker
to spin up a new server on a random assigned port and message back port to the
main thread. The main thread is responsible for proxying the request to the
child server. It works, so it does not block the main thread.
- Run the server
npm run blocking
- Open a separate terminal and run the test
npm run test
- Run the server
npm run threads
- Open a separate terminal and run the test
npm run test
- Run the server
npm run threads-app
- Open a separate terminal and run the test
npm run test