在Angular-Node应用程序中进行CORS调用后,Express会话未持久

时间:2019-05-08 12:05:22

标签: node.js angularjs express cors express-session

我正在尝试创建一个基本的快速会话并将其保存到mongodb。 我被卡住了,因为会话无法通过CORS调用持续进行

我正在使用express-sessions进行此操作,并使用'connect-mongodb-session'将会话保存在'mongodb'中。

我已经进行了正确的配置,并且设法创建了会话,将所需的变量保存在其中,并将会话保存在Mongodb中。

我的进口商品:

const session = require('express-session');
const MongoSessionStore = require('connect-mongodb-session')(session);

我的CORS配置

app.use((req, res, next)=>{
  // The below 2 headers are for cookies
  res.setHeader("Access-Control-Allow-Credentials", true);
  res.setHeader("Access-Control-Allow-Origin", "http://localhost:4200");
  res.setHeader("Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept");
  res.setHeader("Access-Control-Allow-Methods",
    "GET, POST, PUT, PATCH, DELETE, OPTIONS");
  next();
});

对于会话:

const store = new MongoSessionStore({
  uri: MONGO_URI,
  collection: 'sessions',
});

app.use(session({secret: 'secret', resave: false, saveUninitialized: false,
  store: store}));

这是我在一次呼叫中将数据保存在会话中的方式:

router.get('/login', (req, res, next) => {
    req.session.isLoggedIn = true;
    req.session.user = result;
    req.session.save();
    console.log(req.sessionID);
    console.log(req.session);
....}

以上日志的结果:

Session {
  cookie:
  { path: '/',
    _expires: null,
    originalMaxAge: null,
    httpOnly: true },
  isLoggedIn: true,
  user: { _id: '1.6419261913557492',
    name: 'Vegeta',
    email: 'veg@gmail.com',
    password: 'galick',
    cart: { items: [] },
    __v: 0 } }

YCinv0rm8MOFplCHyc5l1z9wtXKVJKTR

现在,在另一个通话中:

router.put('/cart/add/:_id', (req, res, next) => {
  console.log(req.sessionID);
  console.log(req.session);
  ...}

结果:

oL2C7j5HYLF-GJx4bzOl_1_84homq7Lx

Session {
  cookie:
  { path: '/',
    _expires: null,
    originalMaxAge: null,
    httpOnly: true } }
TypeError: Cannot read property 'cart' of undefined
    at router.put (d:\MEAN\shopping\backend\routes\shop-router.js:38:42)
    at Layer.handle [as handle_request] (d:\MEAN\shopping\node_modules\express\lib\router\layer.js:95:5)
    at next (d:\MEAN\shopping\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (d:\MEAN\shopping\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (d:\MEAN\shopping\node_modules\express\lib\router\layer.js:95:5)
    at d:\MEAN\shopping\node_modules\express\lib\router\index.js:281:22
    at param (d:\MEAN\shopping\node_modules\express\lib\router\index.js:354:14)
    at param (d:\MEAN\shopping\node_modules\express\lib\router\index.js:365:14)
    at Function.process_params (d:\MEAN\shopping\node_modules\express\lib\router\index.js:410:3)
    at next (d:\MEAN\shopping\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (d:\MEAN\shopping\node_modules\express\lib\router\index.js:174:3)
    at router (d:\MEAN\shopping\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (d:\MEAN\shopping\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (d:\MEAN\shopping\node_modules\express\lib\router\index.js:317:13)
    at d:\MEAN\shopping\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (d:\MEAN\shopping\node_modules\express\lib\router\index.js:335:12)

我不知道我在做什么错。我检查了关于stackoverflow,github等的类似问题,但没有一个对我有帮助。任何帮助都会很棒。 谢谢

1 个答案:

答案 0 :(得分:0)

找到了答案。似乎对于我通过HttpClient发出的每个服务器请求,我都需要将withCredentials选项设置为true。 像这样:

this.http.get<{message: string, orders: any}>
('http://localhost:3000/orders/get', {withCredentials: true});

我想我只想找到一种全局设置此选项的方法。