使用nodejs和mongodb。
我正在尝试为每个用户登录时创建会话ID。我在app.js文件上有一个会话,但我想在其他地方创建它或者使用db做一些事情?这是我的app.js文件的代码。另外,我也想访问它,我该怎么做呢?我想要做的一种方法是创建一个sessionid并将其保存在一段时间后过期的数据库中,但这可能会浪费空间和服务器时间。
var express = require('express')
, app = express()
, db = require('./models/user')
, pass = require('./config/passport')
, passport = require('passport')
, routes = require('./routes')
, user = require('./routes/user')
, path = require('path')
, http = require('http')
, connect = require('connect')
//, io = require('socket.io') download "socket.io": "0.9.11"
, mongoose = require('mongoose');
var mongoConnect = mongoose.connect('mongodb://localhost/test5');
var Session = require('connect-mongo')(connect);
// all environments
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({
secret: 'settings.cookie_secret',
store: new Session({
url: 'mongodb://localhost/test5',
maxAge: 300000
})
}));
console.log(Session);
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
/*var server = require('http').createServer(app)
, io = io.listen(server);
io.on('connection', function(socket) {
// sends a message socket.emit('news', { hello: 'world' });
socket.on('set nickname', function (name) {
socket.set('nickname', name, function() {socket.emit('ready');});
console.log(name);
});
socket.on('msg', function () {
socket.get('nickname', function (err, name) {
console.log('Chat message by ', name);
});
});
});
*/
app.get('/', routes.index);
app.get('/register', user.register);
app.post('/register', user.registerPost);
app.get('/registerError', user.registerError);
app.get('/registerThanks', user.registerThanks);
app.get('/login', user.login);
app.post('/login', user.loginPost);
app.get('/loginError', user.loginError);
app.get('/userProfile', user.userProfile);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});