-
-
Save cb-v4s/ff9d9773b21de332c12dea8fa629ab77 to your computer and use it in GitHub Desktop.
non-blocking multiprocess express webserver for long time consuming tasks
This file contains hidden or 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 { fork } = require('child_process') | |
| const app = express() | |
| app.get('/', (req, res) => { | |
| const name = req.query.name | |
| const cp = fork('./greetings.js') // heavy task example | |
| cp.send({ "name", name }) | |
| cp.on("message", message => { | |
| res.send(message) | |
| }) | |
| }) | |
| app.listen(PORT, () => console.log(`Listening @ http://localhost:${PORT}`)) |
This file contains hidden or 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
| process.on("message", message => { | |
| const res = Greetings(message.name) | |
| process.send(res) | |
| process.exit() | |
| }) | |
| const Greetings = (name) => return `Hello ${name}!` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment