Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express= require('express');
- const mongoose= require('mongoose');
- const bodyParser= require('body-parser');
- const exphbs= require('express-handlebars');
- const path= require('path');
- const methodOverride= require('method-override');
- const session= require('express-session');
- const flash= require('connect-flash');
- const app= express();
- const port= process.env.PORT || 8000;
- mongoose.Promise= global.Promise;
- mongoose.connect(process.env.MONGODB_URI ||
- 'mongodb://localhost:27017/chat',{ useNewUrlParser: true });
- app.use(express.static(path.join(__dirname, 'public')));
- //body-parser
- app.use(function(req, res, next)
- app.use(bodyParser.urlencoded({extended:true}));
- //View engine
- app.engine('.hbs', exphbs({extname: '.hbs'}));
- app.set('view engine', '.hbs');
- //Load Routes
- const index= require('./routes/index');
- //Use routes
- app.use('/',index);
- app.listen(port,()=> {
- console.log(`Started on port ${port}`);
- })
- const express = require('express');
- const router = express.Router();
- const bcrypt= require('bcryptjs');
- const {User}= require('../models/User');
- router.all('/*',(req,res,next)=> {
- req.app.locals.layout= 'layout';
- next();
- })
- router.get('/login',(req,res,next)=> {
- res.render('routes_UI/login');
- })
- router.get('/signup',(req,res,next)=> {
- res.render('routes_UI/signup');
- })
- router.post('/signup',(req,res)=> {
- let errors=[];
- if(req.body.password!==req.body.confirmPassword){
- errors.push({message:'Passwords do not match'});
- }
- if(errors.length>0){
- res.render('routes_UI/signup',{errors});
- }else{
- User.findOne({ username: req.body.username}).then((user)=> {
- if(user){
- req.flash('error_message',`A user with this username already exists`);
- res.redirect('/signup');
- }else{
- bcrypt.genSalt(10, function(err, salt) {
- bcrypt.hash(req.body.password, salt, function(err, hash) {
- const user= new User({
- username:req.body.username,
- password:hash,
- });
- user.save().then(()=> {
- req.flash('success_message',`You have
- registered successfully, please login`);
- res.redirect('/login');
- });
- });
- });
- }
- })
- }
- })
- module.exports = router;
- <div class="card card-register mx-auto mt-5">
- <div class="card-header">Register an Account</div>
- <div class="card-body">
- <form action="/signup" method="post" enctype="multipart/form-data">
- <div class="form-group">
- <label for="username">Username</label>
- <input name="username" class="form-control" id="username" type="text" aria-describedby="username" placeholder="Enter username">
- </div>
- <div class="form-group">
- <label for="exampleInputPassword1">Password</label>
- <input name="password" class="form-control" id="exampleInputPassword1" type="password" placeholder="Password">
- </div>
- <div class="form-group">
- <label for="exampleConfirmPassword">Confirm password</label>
- <input name="confirmPassword" class="form-control" id="exampleConfirmPassword" type="password" placeholder="Confirm password">
- </div>
- <button type="submit" class="btn btn-primary btn-block">Register</button>
- </form>
- <div class="text-center">
- <a class="d-block small mt-3" href="/login">Login Page</a>
- <a class="d-block small" href="/">Home Page?</a>
- </div>
- </div>
- </div>
- (node:2468) UnhandledPromiseRejectionWarning: ValidationError: users
- validation failed: username: Path `username` is required., password: Path
- `password` is required.
- at new ValidationError (C:UsersDEEPAKchat-app-
- 1node_modulesmongooseliberrorvalidation.js:30:11)
- at model.Document.invalidate (C:UsersDEEPAKchat-app-
- 1node_modulesmongooselibdocument.js:2064:32)
- at p.doValidate.skipSchemaValidators (C:UsersDEEPAKchat-app-
- 1node_modulesmongooselibdocument.js:1927:17)
- at C:UsersDEEPAKchat-app-1node_modulesmongooselibschematype.js:896:9
- at process._tickCallback (internal/process/next_tick.js:61:11)
- (node:2468) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
- This error originated either by throwing inside of an async function
- without a catch block, or by rejecting a promise which was not handled with
- .catch(). (rejection id: 1)
- (node:2468) [DEP0018] DeprecationWarning: Unhandled promise rejections are
- deprecated. In the future, promise rejections that are not handled will
- terminate the Node.js process with a non-zero exit code.
- app.use(bodyParser.json())
Add Comment
Please, Sign In to add comment