Guest User

Untitled

a guest
Dec 24th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. const express= require('express');
  2. const mongoose= require('mongoose');
  3. const bodyParser= require('body-parser');
  4. const exphbs= require('express-handlebars');
  5. const path= require('path');
  6. const methodOverride= require('method-override');
  7. const session= require('express-session');
  8. const flash= require('connect-flash');
  9.  
  10.  
  11. const app= express();
  12. const port= process.env.PORT || 8000;
  13.  
  14. mongoose.Promise= global.Promise;
  15. mongoose.connect(process.env.MONGODB_URI ||
  16. 'mongodb://localhost:27017/chat',{ useNewUrlParser: true });
  17. app.use(express.static(path.join(__dirname, 'public')));
  18.  
  19. //body-parser
  20. app.use(function(req, res, next)
  21. app.use(bodyParser.urlencoded({extended:true}));
  22.  
  23. //View engine
  24. app.engine('.hbs', exphbs({extname: '.hbs'}));
  25. app.set('view engine', '.hbs');
  26.  
  27. //Load Routes
  28. const index= require('./routes/index');
  29.  
  30. //Use routes
  31. app.use('/',index);
  32.  
  33. app.listen(port,()=> {
  34. console.log(`Started on port ${port}`);
  35. })
  36.  
  37. const express = require('express');
  38. const router = express.Router();
  39. const bcrypt= require('bcryptjs');
  40. const {User}= require('../models/User');
  41.  
  42.  
  43. router.all('/*',(req,res,next)=> {
  44. req.app.locals.layout= 'layout';
  45. next();
  46. })
  47.  
  48. router.get('/login',(req,res,next)=> {
  49.  
  50. res.render('routes_UI/login');
  51. })
  52.  
  53. router.get('/signup',(req,res,next)=> {
  54.  
  55. res.render('routes_UI/signup');
  56. })
  57.  
  58.  
  59. router.post('/signup',(req,res)=> {
  60.  
  61. let errors=[];
  62.  
  63. if(req.body.password!==req.body.confirmPassword){
  64. errors.push({message:'Passwords do not match'});
  65. }
  66.  
  67. if(errors.length>0){
  68. res.render('routes_UI/signup',{errors});
  69. }else{
  70.  
  71. User.findOne({ username: req.body.username}).then((user)=> {
  72. if(user){
  73. req.flash('error_message',`A user with this username already exists`);
  74. res.redirect('/signup');
  75. }else{
  76. bcrypt.genSalt(10, function(err, salt) {
  77. bcrypt.hash(req.body.password, salt, function(err, hash) {
  78.  
  79. const user= new User({
  80. username:req.body.username,
  81. password:hash,
  82. });
  83.  
  84. user.save().then(()=> {
  85. req.flash('success_message',`You have
  86. registered successfully, please login`);
  87. res.redirect('/login');
  88. });
  89. });
  90. });
  91. }
  92. })
  93. }
  94. })
  95.  
  96. module.exports = router;
  97.  
  98. <div class="card card-register mx-auto mt-5">
  99. <div class="card-header">Register an Account</div>
  100. <div class="card-body">
  101. <form action="/signup" method="post" enctype="multipart/form-data">
  102. <div class="form-group">
  103. <label for="username">Username</label>
  104. <input name="username" class="form-control" id="username" type="text" aria-describedby="username" placeholder="Enter username">
  105. </div>
  106. <div class="form-group">
  107. <label for="exampleInputPassword1">Password</label>
  108. <input name="password" class="form-control" id="exampleInputPassword1" type="password" placeholder="Password">
  109. </div>
  110. <div class="form-group">
  111. <label for="exampleConfirmPassword">Confirm password</label>
  112. <input name="confirmPassword" class="form-control" id="exampleConfirmPassword" type="password" placeholder="Confirm password">
  113. </div>
  114.  
  115. <button type="submit" class="btn btn-primary btn-block">Register</button>
  116. </form>
  117. <div class="text-center">
  118. <a class="d-block small mt-3" href="/login">Login Page</a>
  119. <a class="d-block small" href="/">Home Page?</a>
  120. </div>
  121. </div>
  122. </div>
  123.  
  124. (node:2468) UnhandledPromiseRejectionWarning: ValidationError: users
  125. validation failed: username: Path `username` is required., password: Path
  126. `password` is required.
  127.  
  128. at new ValidationError (C:UsersDEEPAKchat-app-
  129. 1node_modulesmongooseliberrorvalidation.js:30:11)
  130.  
  131. at model.Document.invalidate (C:UsersDEEPAKchat-app-
  132. 1node_modulesmongooselibdocument.js:2064:32)
  133.  
  134. at p.doValidate.skipSchemaValidators (C:UsersDEEPAKchat-app-
  135. 1node_modulesmongooselibdocument.js:1927:17)
  136.  
  137. at C:UsersDEEPAKchat-app-1node_modulesmongooselibschematype.js:896:9
  138. at process._tickCallback (internal/process/next_tick.js:61:11)
  139. (node:2468) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
  140. This error originated either by throwing inside of an async function
  141. without a catch block, or by rejecting a promise which was not handled with
  142. .catch(). (rejection id: 1)
  143.  
  144. (node:2468) [DEP0018] DeprecationWarning: Unhandled promise rejections are
  145. deprecated. In the future, promise rejections that are not handled will
  146. terminate the Node.js process with a non-zero exit code.
  147.  
  148. app.use(bodyParser.json())
Add Comment
Please, Sign In to add comment