从不同的路线访问时,Express会话是不确定的?

时间:2017-03-22 18:22:15

标签: node.js session express session-variables express-session

我试图实现无限滚动。在我的第一个路径中,我从数据库中加载了5个元素,并存储了上次看到的项目的ID。要加载更多数据,调用getNextHomeContent,但由于某种原因,req.session.lastseen未定义。

server.js

[('x1_idx_enc_a', 0.13874401585637453),
 ('x1_idx_enc_c', 0.23498565469334595),
 ('x2_idx_enc_foo', -0.083558932128022873),
 ('x3', 0.0030186112903237442),
 ('x4', -0.12951394186593695)]

这是我在 homeCtrl.js

中的路线控制器
var express  = require('express');
var session = require('express-session');
var app      = express();
var mongoose = require('mongoose');
var logger = require('morgan');
var bodyParser = require('body-parser');
var cors = require('cors');

//imports database configuration
var databaseConfig = require('./config/database');

//defines route file to use
var router = require('./app/routes');

//connect to datbase
mongoose.connect(databaseConfig.url);

app.listen(process.env.PORT || 8080);
console.log("App listening on port 8080");


app.use(bodyParser.urlencoded({ extended: false })); // Parses urlencoded bodies
app.use(bodyParser.json()); // Send JSON responses
app.use(logger('dev')); // Log requests to API using morgan
app.use(cors());

app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: 'keyboard cat',
  cookie: {
    maxAge: 60000,
    secure: false
  }
}));
router(app);

当获取Home内容时,req.session.lastname将设置为最后一项的ID。但是,当getNextHomeContent()运行时,req.session.lastitem将变为未定义。任何人都可以帮我这个吗?

谢谢!

0 个答案:

没有答案