Last active
March 21, 2020 14:57
-
-
Save getaclue00/62142f5b829c4594a2f13bff7505f90d to your computer and use it in GitHub Desktop.
code to follow along my write up of a bcrypt expressJS example
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
// index.js | |
const express = require('express'); | |
// import bcrypt and set the salt rounds | |
// read about salt in the readme | |
const bcrypt = require('bcrypt'); | |
const bcryptSALTrounds = 10; | |
const app = express(); | |
const PORT = 3000; | |
// REQUIREMENT FUNCTION #1 | |
// http://localhost:3000/hash/dasdas | |
app.get('/hash/:stuff', async (req, res, next) => { | |
// want to inspect params? uncomment : | |
// console.log(req.params); | |
const { stuff } = req.params; | |
const hash = await bcrypt.hash(`${stuff}`, bcryptSALTrounds); | |
res.send({ message: `${hash}` }); | |
}); | |
// REQUIREMENT FUNCTION #2 | |
// http://localhost:3000/compare/$2b$10$DB0melt1B.3Fn7MfXwC17OIevWVhtSgXMrINkg9M4SEHdFgM6Myqm/dasdas | |
app.get('/compare/:hash/:stuff', async (req, res, next) => { | |
// want to inspect params? uncomment : | |
// console.log(req.params); | |
const { hash, stuff } = req.params; | |
const match = await bcrypt.compare(stuff, hash); | |
if (match) { | |
res.send({ message: 'match' }); | |
} else { | |
res.send({ message: `not match` }); | |
} | |
}); | |
app.listen(PORT, () => { | |
console.log(`listening on port : ${PORT}`); | |
}); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment