1

所以,我正在开发 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没有我分配的用户。我在这里做错了什么?

4

1 回答 1

0

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

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

在那之后,一切都是彩虹和蝴蝶。

于 2013-04-19T22:37:44.613 回答