Skip to content

Instantly share code, notes, and snippets.

@mykeels
Created April 6, 2018 15:19
Show Gist options
  • Save mykeels/79ee2c3e9e50b9d4925eda5c065ca035 to your computer and use it in GitHub Desktop.
Save mykeels/79ee2c3e9e50b9d4925eda5c065ca035 to your computer and use it in GitHub Desktop.
const express = require('express')
const crypto = require('crypto')
const isPrime = require('./is-prime')
const app = express()
const calculateHash = function (req, res, buf, encoding){
const hash = crypto.createHash('sha1')
hash.setEncoding('hex')
hash.write(buf.toString(encoding || 'utf8'))
hash.end()
var sha1sum = hash.read()
req.hash = sha1sum
}
const textBodyParser = require('body-parser').text({ verify: calculateHash })
app.post('/', textBodyParser, (req, res) => {
res.send(req.hash)
})
app.get('/', (req, res) => {
const primes = []
const max = Number(req.query.max) || 1000
for (let i = 1; i <= max; i++) {
if (isPrime(i)) primes.push(i)
}
res.json(primes)
})
app.listen(process.env.PORT || 3030)
console.log('app is running!')
module.exports = app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment