会话存储不存储数据

时间:2013-03-22 19:01:25

标签: node.js session express

所以,我正在开发REST API,我正在使用express,mongojs,connect-mongo。我无法弄清楚如何在会话中存储数据:

var express = require('express'),
mongojs = require("mongojs"), 
MongoStore = require('connect-mongo')(express);
GLOBAL.db = mongojs.connect(databaseUrl, collections);   
app.configure(function () {
        app.use(express.bodyParser());
        app.use(express.cookieParser());
        app.use(express.session({
            key: 'express.sid',
            secret: 'secret',
            maxAge: new Date(Date.now() + 3600000),
            store: new MongoStore({ db: databaseUrl, collection: 'sessions' })
        }));
        app.use(app.router);
        app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
     });

用户尝试使用以下方式登录:

curl -X POST http://localhost:2048/auth  -d @./sampleData/authUser.json -H "Content-Type: application/json" --cookie myCookie.txt

当用户成功登录时,我执行:

req.session.user = {}
req.session.user.id = currentUser._id.toString()
req.session.user.userame = currentUser.username
req.session.user.firstName = currentUser.firstName
req.session.user.lastName = currentUser.lastName
req.session.user.title = currentUser.title
req.session.user.location = currentUser.location
res.send(500)

但是当我发送下一个请求时

 curl -X GET http://localhost:2048/projects --cookie myCookie.txt

req.session没有我已经分配过的用户。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

好的问题是我没有那里提到的环境。将第5行更改为:

app.configure('development', function () {

之后是所有的彩虹和蝴蝶。