如何在cookie-session 1.0.2中传递cookie选项 [英] How to pass cookie options in cookie-session 1.0.2

查看:124
本文介绍了如何在cookie-session 1.0.2中传递cookie选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习Node的cookie-session模块。
https://github.com/expressjs/cookie-session

I'm trying to learn the "cookie-session" module for Node. https://github.com/expressjs/cookie-session

我很难理解如何传递cookie的选项。例如过期。默认似乎是一年!

I have a hard time understanding how to pass options for the cookie. For example expiration. Default seems to be a year!

这是关于Cookie选项的说明:
其他选项传递到 Cookie。允许您控制安全性,域,路径和其他设置之间的签名。

This is the instructions about options for the cookie: "Other options are passed to cookies.get() and cookies.set() allowing you to control security, domain, path, and signing among other settings."

但我不明白!
我是否需要cookie模块?
或者我以某种方式更改选项槽 var session = require('cookie-session')
我试过 session.cookies.set(),但是似乎没有工作。

But i dont get it! Am I supposed to require cookies module as well? Or do I somehow change the options trough var session = require('cookie-session')? I have tried session.cookies.set(), but that doesnt seems to work.

我试图在cookie-session和cookies模块中读取源代码的线索,但我不知道要找什么!

I have tried to read the sourcecode in the "cookie-session" and "cookies" module for clues, but I dont know what to look for!

推荐答案

短回答​​



定义要在创建会话,如文档中所示: https://github.com/expressjs/cookie-session 。它们将在创建Cookie(包括到期选项)时使用。

Short answer

Define the options you want to specify in the creation of the session, as illustrated in the docs: https://github.com/expressjs/cookie-session. They will be used when creating the cookie (including the expires option).

app.use(session({
  keys: ['key1', 'key2'],
  secureProxy: true // if you do SSL outside of node
  // more options here...
}))



长回答


b $ b

使用上面的例子,当你将配置对象传递到 session 您要将此对象发送到此处的函数。这个 opts 是传递的,但特别是存储为 req.sessionOptions 此处。创建新会话 req /cookie-session/blob/08c748853b883c87b34aed9d50ce4aff49a173d7/index.js#L109-L110rel =nofollow>并存储为 this._ctx 。最后,当 Session 调用 save 时,这些选项从 sessionOptions 中提取,并在 set 调用cookies

Long answer

Using the example above, when you pass in the configuration object into session, you are sending this object into the function here. This opts is passed around, but in particular, stored as req.sessionOptions here. req is passed in when creating a new Session, and stored as this._ctx. Finally, when save is called on the Session, these options are pulled from the sessionOptions and used in the set call for the cookies:

Session.prototype.save = function(){
  var ctx = this._ctx;
  var json = this._json || encode(this);
  var opts = ctx.sessionOptions;
  var name = ctx.sessionKey;

  debug('save %s', json);
  ctx.sessionCookies.set(name, json, opts);
};

所以你最初传递的选项会传递给 set 在创建cookie时调用。

So the options you pass in originally are passed to the set call when creating the cookie.

这篇关于如何在cookie-session 1.0.2中传递cookie选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆