我使用Express Node.js来创建带有SQL服务器的登录页面,我想使用session。这是我的代码:
echo someline > somefile.txt
我在管理页面上总是有一个/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var admin = require('./routes/admin');
var http = require('http');
var path = require('path');
var bodyParser = require('body-parser');
var session = require('express-session');
var async = require("async");
var edge = require('edge');
var cookieParser = require('cookie-parser');
var app = express();
// all environments
app.set('port', process.env.PORT || 3030);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'assets')));
app.use(express.cookieParser());
app.use(express.session({
cookieName: 'session',
secret: 'dgjdjdfjfjhhytjhd',
duration: 30 * 60 * 1000,
sctiveDuration: 50 * 60 * 1000,
cookie: { maxAge: 24 * 60 * 60 * 1000 }
}));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
///////////// Routs Here .......
app.get('/', function(req, res) {
res.send('<h5>Welcome</h5><a href="login" class="btn btn-default">Go To Login</a>');
});
app.get('/login', function(req, res) {
res.render('login');
});
/*======================================= Post For Login & Redirect To Admin Page =====================================*/
app.post('/login', function (req, res) {
var username = req.body.username;
var password = req.body.password;
var params = {
connectionString: "Data Source=AHMAD-HOME;Initial Catalog=nodejs;Integrated Security=True",
source: "SELECT * FROM users WHERE username='"+ username+"' and password='"+ password+"'"
};
var getUsers = edge.func('sql', params);
getUsers(null, function (error, result) {
if (error) throw error;
if (result) {
if (result == '') {
console.log(' Empty Data From Database');
res.redirect('/login');
} else {
req.session.userid= result;
res.redirect('admin');
}
} else {
console.log('kkkkkkkkkkk');
}
});
console.log('The UserName Is ' + username + ' And The Password Is : ' + password);
});
app.get('/admin', function (req, res) {
res.render('admin');
console.log(req.session);// stack here
});
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
会话。
我使用了快速会话,之前我尝试过客户端会话,但我仍然是同样的错误。
答案 0 :(得分:0)
app.use(**express.**session({
cookieName: 'session',
secret: 'dgjdjdfjfjhhytjhd',
duration: 30 * 60 * 1000,
sctiveDuration: 50 * 60 * 1000,
cookie: { maxAge: 24 * 60 * 60 * 1000 }
}));
删除**中的部分。这在Express的最新版本中已被弃用。您必须使用已经设置并分配给session
的中间件快速会话。