2014-03-18 138 views
3

只是無法找出這一個。我發現的關於快速會話的一切都說明它只是起作用,但是,對我而言並非如此。nodejs express req.session undefined

我的整個應用程序的配置

app.configure -> 
    app.set 'port', process.env.PORT || 3000 
    app.set 'views', path.join(__dirname, 'views') 
    app.set 'view engine', 'jade' 
    app.use express.favicon() 
    app.use express.logger('dev') 
    app.use express.json() 
    app.use express.urlencoded() 
    app.use express.methodOverride() 
    app.use app.router 
    app.use express.static(path.join(__dirname, 'public')) 
    app.use express.bodyParser() 
    app.use express.cookieParser() 
    sessionStore = new express.session.MemoryStore 
    app.use express.session secret: 'key', store: sessionStore 

req.session只是undefined。我想用它來進行用戶認證 - 非常基本。出於某種原因,對我來說,這變得非常艱難。我正在使用express 3.5.0

我控制器ATM看起來是這樣的:

class AuthenticationController 

    signUp: (req, res) -> 
    console.log req.body._id 
    console.log req.session 
    res.status(200).send req.body 

module.exports = AuthenticationController 

和路線:

format = require './middlewares/format' 
AuthenticationController = require './controller' 

class Routes 

    constructor: (app) -> 
    throw new Error('app undefined') unless app 
    @controller = new AuthenticationController 
    @app = app 
    route.apply @ for key, route of @routes 

    routes: 
    sign_up: -> 
     @app.post '/sign_up', format.onlyJSON, @controller.signUp 

module.exports = Routes 
+0

顯示你使用'req'的代碼? – Ali

+2

在會話中間件之前有'app.use app.router',所以所有的路由都將在會話建立之前處理。你爲什麼要這樣訂購? – loganfsmyth

+0

沒想到這很重要,實際上...... –

回答

6

正如loganfsmyth建議剛動

app.use express.cookieParser() 
sessionStore = new express.session.MemoryStore 
app.use express.session secret: 'key', store: sessionStore 

app.use app.router以上,它只是點擊。謝謝。