我有一个用快递3编写的服务器,我使用EJS作为模板引擎,我想知道如何在我的视图中显示我的会话变量。
app.js / ** *模块依赖性。 * /
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({secret : 'xvxcvxvcxvxcv'}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
var ck_username = /^[A-Za-z0-9_]{1,20}$/;
var ck_password = /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
app.post("/Register",function(req, res){
var username = req.body.username;
var password = req.body.password;
//var confirmpassword = req.body.confirmpassword;
var email = req.body.email;
var errors = [];
if (!ck_email.test(email)) {
errors[errors.length] = "Invalid email adress";
}
if (!ck_username.test(username)) {
errors[errors.length] = "Invalid username .";
}
if (!ck_password.test(password)) {
errors[errors.length] = "Invalid password ";
}
if (errors.length > 0) {
req.session.response = "Erors Occured While Registration!";
res.redirect('/Signup');
}
else{
req.session.response = "Account Successfully Created";
res.redirect('/Signup');
}
});
app.get("/Signup", function(req, res){
res.render('Signup.ejs', {
layout:false
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
会话已创建我可以在firebug中看到它但是我没有看到如何在我的视图中显示它,我做了如下,我收到此错误:
500 ReferenceError: C:\Users\user\workingFolder\views\Signup.ejs:27 25| <div> 26| <form action="/Register" method="post"> >> 27| <div class="error-message"><%= response %></div> 28| <label for="username">Username<b style="color:red;">*</b>:</label> 29| <input type="text" id="username" name="username"><br> 30| <label for="password">Password<b style="color:red;">*</b>:</label> response is not defined
这是我的观看代码:
....................
....................
<form action="/Register" method="post">
<div class="error-message"><%= response %></div>
<label for="username">Username<b style="color:red;">*</b>:</label>
<input type="text" id="username" name="username"><br>
<label for="password">Password<b style="color:red;">*</b>:</label>
<input type="password" id="password" name="password"><br>
<label for="confirmpassword">Confirm Password<b style="color:red;">*</b>:</label>
<input type="password" id="confirmpassword" name="confirmpassword"><br>
<label for="email">Email<b style="color:red;">*</b>:</label>
<input type="text" id="email" name="email"><br>
<input type="submit" value="Register">
</form>
................
................
答案 0 :(得分:7)
如果您想查看调试会话,我建议使用express-debug工具栏,在这里:https://github.com/devoidfury/express-debug
否则,您需要将req.session传递给模板上下文,并可能将其输出为JSON。
res.render('Signup.ejs', {
layout:false,
session: req.session
});
并在您的模板中:
<%= JSON.stringify(session) %>