Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. const express = require('express');
  2. // const data = require('./data');
  3. const MongoClient = require('mongodb').MongoClient();
  4. const assert = require('assert');
  5. const path = require('path');
  6. const bodyParser = require('body-parser');
  7. const mustacheExpress = require('mustache-express');
  8. const bcrypt = require('bcryptjs');
  9. const app = express();
  10. const profileController = require('./controllers/profile');
  11. const unemployedController = require('./controllers/unemployed');
  12. const employedController = require('./controllers/employed');
  13.  
  14. // Mustache Boiler Plate
  15. app.engine('mustache', mustacheExpress());
  16. app.set('views', './views');
  17. app.set('view engine', 'mustache');
  18.  
  19. // Setting up the style sheet
  20. app.use(express.static('public'));
  21.  
  22. app.use(bodyParser.json());
  23. app.use(bodyParser.urlencoded({extended: true}));
  24.  
  25. // Mongo Boiler Plate
  26.  
  27. // Connection Url
  28. var url = 'mongodb://localhost:27017/return-dir';
  29.  
  30. // !! Only run this once to set up mongo
  31. // Use connect method to conenct to the server
  32. // MongoClient.connect(url)
  33. // .then(function(db) {
  34. // console.log("Connected Mongo to the Server!");
  35. // return db.collection("users").insertMany(data.users);
  36. // });
  37.  
  38. app.get('/', function(req, res){
  39. MongoClient.connect(url)
  40. .then(function(db) {
  41. db.collection('users')
  42. .find().toArray()
  43. .then(function(data) {
  44. db.close();
  45. // console.log(data);
  46. res.render('index', {users: data});
  47. });
  48. });
  49. });
  50. // Route to find hardcoded hash
  51. // app.get('/hash', function(req, res){
  52. // let hash = bcrypt.hashSync('1234', 8);
  53. // console.log(hash);
  54. // // $2a$08$.on267yC/P7etBmub4MUG.HCvqA/bVE0MjXxnwpZjopQ3KyfadOxO
  55. // });
  56.  
  57. app.get('/login', function(req, res){
  58. res.render('login');
  59. });
  60.  
  61. app.post('/login', function(req, res){
  62. let username = req.body.username
  63. let password = req.body.password
  64. MongoClient.connect(url)
  65. .then(function(db){
  66. db.collection('users')
  67. .findOne({username: username})
  68. .then(function(data){
  69. db.close()
  70. if(bcrypt.compareSync(password, data.passwordHash)){
  71.  
  72. res.redirect('/');
  73. } else {
  74. res.send('nope')
  75. }
  76. })
  77. })
  78. });
  79.  
  80. app.get('/create/user', function(req, res){
  81. res.render('createuser');
  82. });
  83.  
  84. app.post('/create/user', function(req, res){
  85. let username = req.body.username;
  86. let password = bcrypt.hashSync(req.body.password, 8);
  87. MongoClient.connect(url)
  88. .then(function(db){
  89. db.collection('users')
  90. .insertOne({username: username, passwordHash: password})
  91. .then(function(user){
  92. console.log(user);
  93. })
  94. // let user = db.collection('users').findOne({username: username});
  95. // console.log(user);
  96. db.close();
  97. });
  98. });
  99.  
  100. app.use('/unemployed', unemployedController);
  101. app.use('/employed', employedController);
  102. app.use('/profile', profileController);
  103.  
  104. app.listen(3000, function(){
  105. console.log('Rage Against the (Employable) Machine');
  106. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement