Skip to content

Instantly share code, notes, and snippets.

@cb-v4s
Created July 16, 2021 21:11
Show Gist options
  • Select an option

  • Save cb-v4s/ff9d9773b21de332c12dea8fa629ab77 to your computer and use it in GitHub Desktop.

Select an option

Save cb-v4s/ff9d9773b21de332c12dea8fa629ab77 to your computer and use it in GitHub Desktop.
non-blocking multiprocess express webserver for long time consuming tasks
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}`))
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