2017-04-06 58 views
-1

這是簡單的登錄表單後。我驗證了一封電子郵件,如果電子郵件無效,它會發出錯誤(這裏沒有問題)。在刷新該頁面而不是顯示登錄表單之後,它還顯示錯誤以及該頁面上的登錄表單。會議的NodeJS刷新頁面

app.js

var express = require('express'); 
var path = require('path'); 
var favicon = require('serve-favicon'); 
var logger = require('morgan'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 
var hbs = require('express-handlebars'); 
var expressValidator = require('express-validator'); 
var expressSession = require('express-session'); 

var routes = require('./routes/index'); 

var app = express(); 

// view engine setup 
//app.engine('hbs', hbs({extname: 'ejs'})); 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'ejs'); 

//app.set('views', path.join(__dirname, 'views')); 
//app.set('view engine', 'hbs'); 

// uncomment after placing your favicon in /public 
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 
app.use(logger('dev')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(expressValidator()); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 
app.use(expressSession({secret: 'max', saveUninitialized: false, resave: false})); 

app.use('/', routes); 

// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 

// error handlers 

// development error handler 
// will print stacktrace 
if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
     message: err.message, 
     error: err 
    }); 
    }); 
} 

// production error handler 
// no stacktraces leaked to user 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
    message: err.message, 
    error: {} 
    }); 
}); 


module.exports = app; 

index.js文件

var express = require('express'); 
var router = express.Router(); 

/* GET home page. */ 
router.get('/', function(req, res, next) { 
    console.log(req.session); 
    res.render('student_login', { success: req.session.success, errors: req.session.errors }); 
    req.session.errors = null; 
    console.log(req.session); 
}); 

router.get('/submit', function(req, res, next) { 
    req.check('e', 'Invalid email address').isEmail(); 
    //req.check('p', 'Password is invalid').isLength({min: 4}).equals(req.body.confirmPassword); 

    var errors = req.validationErrors(); 
    if (errors) { 
    console.log(req.session); 
    req.session.errors = errors; 
    req.session.success = false; 
    console.log(req.session); 
    } else { 
    console.log(req.session); 
    req.session.success = true; 
    console.log(req.session); 
    } 
    res.redirect('/'); 
}); 

module.exports = router; 

student_login.ejs

<!DOCTYPE html> 
<html > 

    <% if(success){ %> 
     <h1>validation succesful</h1> 
    <% } 
    else{ %> 
     <ul> 
     <% if(errors) {%> 
      <h1> errors </h1> 
     <% } %> 
     </ul> 
    <h1>Login</h1> 
    <form action="/submit" method="get"> 
     <input type="text" name="e" placeholder="Email" required="required" > <br> 
     <input type="password" name="p" placeholder="Password" required="required" > <br> 
     <button type="submit" >Let me in.</button> 
    </form> 

<% } %> 
</html> 
+0

你能正確地格式化你的代碼嗎? –

+0

我已經格式化它正確,請檢查一下 –

+0

表達本身不具有請求對象。檢查()方法 –

回答

1

這裏的問題是,你是在會話保存您的錯誤,意味着它將保留在會話中,除非您清除它或會話結束。

你所需要的就是提示信息。這些是存儲在會話中的類似消息,但是一旦向用戶顯示就會清除它們,因此它們只顯示一次。

我以前使用過connect-flash節點模塊。這很容易建立和完成這項工作。