-
-
Save dpjayasekara/893964faa1f7d4badc54c3f8722c6409 to your computer and use it in GitHub Desktop.
Multi-threaded with BCrypt server with dynamically spawned workers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const {Worker, isMainThread, parentPort, workerData} = require('worker_threads'); | |
const encrypt = require('./encrypt'); | |
if (isMainThread) { | |
const app = express(); | |
app.get('/', (req, res, next) => { | |
console.log('serving request...'); | |
const worker = new Worker(__filename, {workerData: req.query.text}); | |
worker.once('message', (enc) => { | |
res.status(200).send(enc); | |
}); | |
worker.once('error', (err) => { | |
console.error(err); | |
}); | |
}); | |
app.listen(8082); | |
} else { | |
const enc = encrypt(workerData); | |
parentPort.postMessage(enc); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment