我有一个用 express 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>
................
................