Node.js 使用同构获取时的新会话ID

Node.js 使用同构获取时的新会话ID,node.js,express,express-session,Node.js,Express,Express Session,在使用与Express会话(尤其是Express会话ID)相关的同构获取与请求承诺时,我有一个意外行为: 作为故障排除过程的一部分,我在client.js中实现了两种方法来调用server.js中的端点:1)同构获取,2)请求承诺 Client.js // Method 1: isomorphic-fetch require('es6-promise').polyfill(); require('isomorphic-fetch'); fetch('http://localhost:3000'

在使用与Express会话(尤其是Express会话ID)相关的同构获取与请求承诺时,我有一个意外行为:

作为故障排除过程的一部分,我在
client.js
中实现了两种方法来调用
server.js
中的端点:1)同构获取,2)请求承诺

Client.js

// Method 1: isomorphic-fetch
require('es6-promise').polyfill();
require('isomorphic-fetch');

fetch('http://localhost:3000', {
  credentials: 'same-origin',
})
.then(function(response) {
  console.log(response.status);
});

fetch('http://localhost:3000', {
  credentials: 'same-origin',
})
.then(function(response) {
  console.log(response.status);
});

// Method 2: request-promise
var rp = require('request-promise').defaults({
 jar: true
});

function requestPage() {
  return rp('http://localhost:3000/');
}
requestPage()
  .then(console.dir)
  .then(requestPage)
  .then(console.dir)
  .catch(console.error);
Server.js

var app = require('express')();
app.use(require('morgan')('dev'));
var session = require('express-session');
var FileStore = require('session-file-store')(session);

app.use(session({
  name: 'server-session-cookie-id',
  secret: 'my express secret',
  saveUninitialized: true,
  resave: true,
  store: new FileStore(),
  cookie: {
    secure: false
  }
}));

app.get('/', function initViewsCount(req, res, next) {
console.log('req.session.id = ' + req.session.id);
  if (typeof req.session.views === 'undefined') {
    req.session.views = 1;
    return res.end('Welcome to the file session demo. Refresh page!');
  }
  return next();
});

app.get('/', function incrementViewsCount(req, res, next) {
  console.assert(typeof req.session.views === 'number',
  'missing views count in the session', req.session);
  req.session.views++;
  return next();
})

app.use(function printSession(req, res, next) {
  console.log('req.session', req.session);
  return next();
});

app.get('/', function sendPageWithCounter(req, res) {
  res.setHeader('Content-Type', 'text/html');
  res.write('<p>views: ' + req.session.views + '</p>\n');
  res.end();
});

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://%s:%s', host, port);
});
我确认方法2(请求承诺)成功地将会话持久化到服务器上。换句话说,会话
a3ktlmdbopq7pafctsjhnzdokda7hgi
与此方法相关联

但是,从输出中可以看出,方法1(同构获取)在服务器上生成两个单独的会话

问题:为什么同构获取在服务器上创建两个单独的会话

已执行故障排除

  • 我用
    127.0.0.1
    替换了
    localhost
    ,但这并没有改变行为
  • 我用
    include
    替换了
    same origin
    ,但这并没有改变行为
  • 环境:

    • 节点v6.10.3
    • 同构fetch 2.2.1
    • 请求承诺4.2.2

    你有没有想过?我也有类似的问题。在服务器上运行fetch时,cookie和原始请求数据似乎丢失了。由于没有cookie数据,express将创建一个新会话。
    req.session.id = deWKCvqcyGiAvVSUvHv2Db7sjvE7xN1E
    req.session.id = MxLHWjbMMvV4GRfPSf6sQ12XvauiJJot
    req.session.id = A3KTLMdBopQ7pAfcTsJhnzzdokdA7hGI
    GET / 200 1.407 ms - -
    GET / 200 7.625 ms - -
    GET / 200 0.728 ms - -
    req.session.id = A3KTLMdBopQ7pAfcTsJhnzzdokdA7hGI
    req.session Session {
      cookie:
       { path: '/',
         _expires: null,
         originalMaxAge: null,
         httpOnly: true,
         secure: false },
      views: 2,
      __lastAccess: 1517449125197 }
    GET / 200 6.902 ms - -