Skip to content

Instantly share code, notes, and snippets.

@mykeels
Created April 4, 2018 11:51
Show Gist options
  • Select an option

  • Save mykeels/435666f076b6eb3d4aa66bbf935c6311 to your computer and use it in GitHub Desktop.

Select an option

Save mykeels/435666f076b6eb3d4aa66bbf935c6311 to your computer and use it in GitHub Desktop.
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
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