Created
April 4, 2018 11:51
-
-
Save mykeels/435666f076b6eb3d4aa66bbf935c6311 to your computer and use it in GitHub Desktop.
Fork of https://gist.github.com/mykeels/8396f87d42a809697d08f6b6c9cd8db0 using child-process via https://github.com/rvagg/node-worker-farm
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 os = require('os') | |
| const app = express() | |
| const workerFarm = require('worker-farm') | |
| const worker = workerFarm(require.resolve('./worker.js')) | |
| app.get('/', (req, res) => { | |
| const max = Number(req.query.max) || 1000 | |
| worker(max, (err, primes) => { | |
| if (err) res.status(500).send(err) | |
| else res.json(primes) | |
| }) | |
| }) | |
| app.listen(process.env.PORT || 3030) | |
| console.log('app is running!') | |
| module.exports = app |
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 isPrime = require('./is-prime') | |
| module.exports = function (max = 0, callback = (err, result) => ({})) { | |
| try { | |
| const primes = [] | |
| for (let i = 1; i <= max; i++) { | |
| if (isPrime(i)) primes.push(i) | |
| } | |
| callback(null, primes) | |
| } | |
| catch (ex) { | |
| callback(ex) | |
| console.error(ex) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment