Created
April 6, 2018 15:19
-
-
Save mykeels/79ee2c3e9e50b9d4925eda5c065ca035 to your computer and use it in GitHub Desktop.
Fork of https://gist.github.com/mykeels/8396f87d42a809697d08f6b6c9cd8db0 showing request hashing
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 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