7

How to redirect to the next page in Express js

 2 years ago
source link: https://www.codesd.com/item/how-to-redirect-to-the-next-page-in-express-js.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to redirect to the next page in Express js

advertisements
 index.hbs

    <h1> {{LoginTitle}} </h1>
      <form action="/login" method="post">
       <label for="Email">Email</label>
         <input type="text" name="Email" id="Email" placeholder="Email">
        <label for="password">password</label>
         <input type="text" name="password" id="password
            placeholder="password">
         <input type ="button" value="SignIn"  onClick="'/welcome'">
        </form>

index.js

var express = require('express');
var router = express.Router();
var mongo = require('mongodb');
var url = 'mongodb://localhost:27017/test';

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { LoginTitle: 'HOUSEHOLD EXPENSE' });
});

router.get('/login', function(req, res, next) {
res.render('welcome', { welcome : 'hey' });
});

router.post('/login', function(req,res,next){
if(req.body.Email == Email && req.body.password == password){
    res.render('welcome');
 }
 else
 {
  res.render('/');
  }

  });

  module.exports = router;

welcome.hbs

  <h1> {{ welcome }}</h1>

  <p> Hello Mr User</p>

I am using express handlebars.After successful logging in it is not going to next page welcome.hbs Can anyone let me know what mistake I am doing


if(req.body.Email == Email && req.body.password == password){
res.render('welcome');
}

should be something like

if(req.body.Email == Email && req.body.password == password){
res.render('welcome', { welcome : 'hey' });
}

In index.hbs

<input type="text" name="password" id="password" placeholder="password">
<input type ="submit" value="SignIn">




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK