{"id":5162,"date":"2016-07-07T11:40:14","date_gmt":"2016-07-07T03:40:14","guid":{"rendered":"https:\/\/sdeno.com\/?p=5162"},"modified":"2016-07-07T11:41:14","modified_gmt":"2016-07-07T03:41:14","slug":"node-js%e4%b8%adexpress-session%e4%b8%ad%e6%96%87%e9%85%8d%e7%bd%ae%e9%a1%b9%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5162","title":{"rendered":"node.js\u4e2dexpress-session\u4e2d\u6587\u914d\u7f6e\u9879\u8be6\u89e3"},"content":{"rendered":"<p><span style=\"color: #555555;\">\u4f5c\u7528\uff1a\u7528\u6307\u5b9a\u7684\u53c2\u6570\u521b\u5efa\u4e00\u4e2asession\u4e2d\u95f4\u4ef6\uff0csesison\u6570\u636e\u4e0d\u662f\u4fdd\u5b58\u5728cookie\u4e2d\uff0c\u4ec5\u4ec5sessionID\u4fdd\u5b58\u5230cookie\u4e2d\uff0csession\u7684\u6570\u636e\u4ec5\u4ec5\u4fdd\u5b58\u5728\u670d\u52a1\u5668\u7aef\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u8b66\u544a\uff1a\u9ed8\u8ba4\u7684\u670d\u52a1\u5668\u7aef\u7684session\u5b58\u50a8\uff0cMemoryStore\u4e0d\u662f\u4e3a\u4e86\u751f\u4ea7\u73af\u5883\u521b\u5efa\u7684\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f1a\u5185\u5b58\u6cc4\u9732\uff0c\u4e3b\u8981\u7528\u4e8e\u6d4b\u8bd5\u548c\u5f00\u53d1\u73af\u5883\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u63a5\u53d7\u7684\u53c2\u6570\uff1a\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">cookie<\/strong><span style=\"color: #555555;\">:\u4e5f\u5c31\u662fsession ID\u7684cookie\uff0c\u9ed8\u8ba4\u662f{ path: &#8216;\/&#8217;, httpOnly: true, secure: false, maxAge: null }.\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">genid<\/strong><span style=\"color: #555555;\">:\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684sessionID\u7684\u51fd\u6570\uff0c\u4e00\u4e2a\u8fd4\u56de\u503c\u662fstring\u7c7b\u578b\u7684\u51fd\u6570\u4f1a\u88ab\u4f5c\u4e3asessionID.\u8fd9\u4e2a\u51fd\u6570\u7b2c\u4e00\u4e2a\u53c2\u6570\u662freq,\u6240\u4ee5\u5982\u679c\u4f60\u60f3\u8981req\u4e2d\u7684\u53c2\u6570\u4ea7\u751fsessionID\u8fd8\u662f\u5f88\u4e0d\u9519\u7684\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u9ed8\u8ba4\u51fd\u6570\u662f\u4f7f\u7528\u00a0<\/span><a style=\"color: #555555;\" href=\"https:\/\/www.npmjs.com\/package\/uid-safe#uidbytelength-callback\" target=\"_blank\" rel=\"nofollow\">uid-safe<\/a><span style=\"color: #555555;\">\u8fd9\u4e2a\u5e93\u4ea7\u751fid\u503c(\u4ea7\u751f\u4e00\u4e2a\u7b97\u6cd5\u4e0a\u5b89\u5168\u7684UID\uff0c\u53ef\u4ee5\u7528\u4e8ecookie\u4e5f\u53ef\u4ee5\u7528\u4e8eURL\u3002\u548crand-token\u548cuid2\u76f8\u6bd4\uff0c\u540e\u8005\u7531\u4e8e\u4f7f\u7528\u4e86%\u5bfc\u81f4UID\u4ea7\u751f\u504f\u6001\uff0c\u540c\u65f6\u53ef\u80fd\u5bf9UID\u4ea7\u751f\u4e0d\u5fc5\u8981\u7684\u622a\u65ad\u3002\u6211\u4eec\u7684uid-safe\u4f7f\u7528\u7684\u662fbase64\u7b97\u6cd5\uff0c\u5176\u51fd\u6570uid(byteLength, callback)\u4e2d\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6bd4\u7279\u957f\u5ea6\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u957f\u5ea6)\u00a0<\/span><\/p>\n<pre class=\"javascript hljs\" style=\"color: #000000;\">       app.use(session({\r\n          genid: <span class=\"hljs-function\" style=\"font-weight: inherit; font-style: inherit;\"><span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">function<\/span><span class=\"hljs-params\" style=\"font-weight: inherit; font-style: inherit; color: #0000ff;\">(req)<\/span> <\/span>{\r\n            <span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">return<\/span> genuuid() <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/ use UUIDs for session IDs <\/span>\r\n          },\r\n          secret: <span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'keyboard cat'<\/span>\r\n        })<\/pre>\n<p><span style=\"color: #555555;\">\u00a0\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">name<\/strong><span style=\"color: #555555;\">:\u5728response\u4e2dsessionID\u8fd9\u4e2acookie\u7684\u540d\u79f0\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2aname\u8bfb\u53d6\uff0c\u9ed8\u8ba4\u662fconnect.sid\u3002\u5982\u679c\u4e00\u53f0\u673a\u5668\u4e0a\u6709\u591a\u4e2aapp\u8fd0\u884c\u5728\u540c\u6837\u7684hostname+port\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u90a3\u4e48\u4f60\u9700\u8981\u5bf9\u8fd9\u4e2asessin\u7684cookie\u8fdb\u884c\u5207\u5272\uff0c\u6240\u4ee5\u6700\u597d\u7684\u65b9\u6cd5\u8fd8\u662f\u901a\u8fc7name\u8bbe\u7f6e\u4e0d\u540c\u7684\u503c\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">resave<\/strong><span style=\"color: #555555;\">:\u5f3a\u5236session\u4fdd\u5b58\u5230session store\u4e2d\u3002\u5373\u4f7f\u5728\u8bf7\u6c42\u4e2d\u8fd9\u4e2asession\u6ca1\u6709\u88ab\u4fee\u6539\u3002\u4f46\u662f\u8fd9\u4e2a\u5e76\u4e0d\u4e00\u5b9a\u662f\u5fc5\u987b\u7684\uff0c\u5982\u679c\u5ba2\u6237\u7aef\u6709\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u4e24\u4e2a\u5e76\u884c\u7684\u8bf7\u6c42\u5230\u4f60\u7684\u5ba2\u6237\u7aef\uff0c\u4e00\u4e2a\u8bf7\u6c42\u5bf9session\u7684\u4fee\u6539\u53ef\u80fd\u88ab\u53e6\u5916\u4e00\u4e2a\u8bf7\u6c42\u8986\u76d6\u6389\uff0c\u5373\u4f7f\u7b2c\u4e8c\u4e2a\u8bf7\u6c42\u5e76\u6ca1\u6709\u4fee\u6539sesion\u3002\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u9ed8\u8ba4\u662ftrue,\u4f46\u662f\u9ed8\u8ba4\u503c\u5df2\u7ecf\u8fc7\u65f6\uff0c\u56e0\u6b64\u4ee5\u540edefault\u53ef\u80fd\u4f1a\u88ab\u4fee\u6539\u3002\u56e0\u6b64\u597d\u597d\u7814\u7a76\u4f60\u7684\u9700\u6c42\u9009\u62e9\u4e00\u4e2a\u6700\u9002\u7528\u7684\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u53ef\u80fd\u9700\u8981false\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u6700\u597d\u7684\u77e5\u9053\u4f60\u7684store\u662f\u5426\u9700\u8981\u8bbe\u7f6eresave\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u67e5\u770b\u4f60\u7684store\u662f\u5426\u5b9e\u73b0\u4e86touch\u65b9\u6cd5(\u5220\u9664\u90a3\u4e9b\u7a7a\u95f2\u7684session\u3002\u540c\u65f6\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u4f1a\u901a\u77e5session store\u6307\u5b9a\u7684session \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u662f\u6d3b\u52a8\u6001\u7684)\uff0c\u5982\u679c\u5b9e\u73b0\u4e86\u90a3\u4e48\u4f60\u53ef\u4ee5\u7528resave:false,\u5982\u679c\u6ca1\u6709\u5b9e\u73b0touch\u65b9\u6cd5\uff0c\u540c\u65f6\u4f60\u7684store\u5bf9\u4fdd\u5b58\u7684session\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u8fc7\u671f\u7684\u65f6\u95f4\uff0c\u90a3\u4e48\u5efa\u8bae\u4f60\u7528resave:true\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">rolling<\/strong><span style=\"color: #555555;\">:\u5f3a\u5236\u5728\u6bcf\u4e00\u4e2aresponse\u4e2d\u90fd\u53d1\u9001session\u6807\u8bc6\u7b26\u7684cookie(\u5982css\u6587\u4ef6\u7684set-cookie\u54cd\u5e94\u5934)\u3002\u5982\u679c\u628aexpiration\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u8fc7\u53bb\u7684\u65f6\u95f4\u90a3\u4e48\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u90a3\u4e48\u8fc7\u671f\u65f6\u95f4\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u7684\u503c\u3002roling\u9ed8\u8ba4\u662ffalse\u3002\u5982\u679c\u628a\u8fd9\u4e2a\u503c\u8bbe\u7f6e\u4e3atrue\u4f46\u662fsaveUnitialized\u8bbe\u7f6e\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u4e3afalse,\u90a3\u4e48cookie\u4e0d\u4f1a\u88ab\u5305\u542b\u5728\u54cd\u5e94\u4e2d(\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">\u6ca1\u6709\u521d\u59cb\u5316\u7684sessio<\/strong><span style=\"color: #555555;\">n)\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">saveUninitialized<\/strong><span style=\"color: #555555;\">:\u5f3a\u5236\u6ca1\u6709\u201c\u521d\u59cb\u5316\u201d\u7684session\u4fdd\u5b58\u5230storage\u4e2d\uff0c\u6ca1\u6709\u521d\u59cb\u5316\u7684session\u6307\u7684\u662f\uff1a\u521a\u88ab\u521b\u5efa\u6ca1\u6709\u88ab\u4fee\u6539\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u5982\u679c\u662f\u8981\u5b9e\u73b0\u767b\u9646\u7684session\u90a3\u4e48\u6700\u597d\u8bbe\u7f6e\u4e3afalse(reducing server storage usage, or complying with laws that require permission before setting a cookie)\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u800c\u4e14\u8bbe\u7f6e\u4e3afalse\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff0c\u5f53\u5ba2\u6237\u7aef\u6ca1\u6709session\u7684\u60c5\u51b5\u4e0b\u5e76\u884c\u53d1\u9001\u591a\u4e2a\u8bf7\u6c42\u65f6\u3002\u9ed8\u8ba4\u662ftrue,\u4f46\u662f\u4e0d\u5efa\u8bae\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">secret<\/strong><span style=\"color: #555555;\">:\u7528\u4e8e\u5bf9sessionID\u7684cookie\u8fdb\u884c\u7b7e\u540d\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2astring(\u4e00\u4e2asecret)\u6216\u8005\u6570\u7ec4(\u591a\u4e2asecret)\u3002\u5982\u679c\u6307\u5b9a\u4e86\u4e00\u4e2a\u6570\u7ec4\u90a3\u4e48\u53ea\u4f1a\u7528\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u7b2c\u4e00\u4e2a\u5143\u7d20\u5bf9sessionID\u7684cookie\u8fdb\u884c\u7b7e\u540d\uff0c\u5176\u4ed6\u7684\u7528\u4e8e\u9a8c\u8bc1\u8bf7\u6c42\u4e2d\u7684\u7b7e\u540d\u3002\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">store<\/strong><span style=\"color: #555555;\">:\u4fdd\u5b58session\u7684\u5730\u65b9\uff0c\u9ed8\u8ba4\u662f\u4e00\u4e2aMemoryStore\u5b9e\u4f8b\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0\u00a0<\/span><strong style=\"font-weight: bold; color: #555555;\">unset<\/strong><span style=\"color: #555555;\">:\u5bf9\u6ca1\u6709\u8bbe\u7f6e\u7684req.session\u8fdb\u884c\u63a7\u5236\uff0c\u901a\u8fc7delete\u6216\u8005\u8bbe\u7f6e\u4e3anull\u3002\u9ed8\u8ba4\u662fkeep,destory\u8868\u793a\u5f53\u56de\u5e94\u7ed3\u675f\u540e\u4f1a\u9500\u6bc1session\uff0ckeep\u8868\u793asession\u4f1a\u88ab\u4fdd\u5b58\u00a0<\/span><\/p>\n<p style=\"color: #555555;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u4f46\u662f\u5728\u8bf7\u6c42\u4e2d\u5bf9session\u7684\u4fee\u6539\u4f1a\u88ab\u5ffd\u7565\uff0c\u4e5f\u4e0d\u4f1a\u4fdd\u5b58<\/p>\n<p style=\"color: #555555;\">\u5728\u7248\u672c1.5.0\u540e\uff0ccookie-parser\u8fd9\u4e2a\u4e2d\u95f4\u4ef6\u5df2\u7ecf\u4e0d\u662fexpress-session\u5de5\u4f5c\u5fc5\u987b\u7684\u4e86\u3002\u8fd9\u4e2a\u6a21\u5757\u53ef\u4ee5\u76f4\u63a5\u5bf9req\/res\u4e2d\u7684cookie\u8fdb\u884c\u8bfb\u5199\uff0c\u4f7f\u7528cookie-parser\u53ef\u80fd\u5bfc\u81f4\u4e00\u4e9b\u95ee\u9898\uff0c\u7279\u522b\u662f\u5f53secret\u5728\u4e24\u4e2a\u6a21\u5757\u4e4b\u95f4\u5b58\u5728\u4e0d\u4e00\u81f4\u7684\u65f6\u5019\u3002<br \/>\n\u8bf7\u628asecure\u8bbe\u7f6e\u4e3atrue\uff0c\u8fd9\u662f\u660e\u667a\u7684\u3002\u4f46\u662f\u8fd9\u9700\u8981\u7f51\u7ad9\u7684\u652f\u6301\uff0c\u56e0\u4e3asecure\u9700\u8981HTTPS\u7684\u534f\u8bae\u3002\u5982\u679c\u8bbe\u7f6e\u4e86secure\uff0c\u4f46\u662f\u4f60\u4f7f\u7528HTTP\u8bbf\u95ee\uff0c\u90a3\u4e48cookie\u4e0d\u4f1a\u88ab\u8bbe\u7f6e\uff0c\u5982\u679cnode.js\u8fd0\u884c\u5728\u4ee3\u7406\u4e0a\uff0c\u540c\u65f6\u4f7f\u7528\u4e86secure\uff1atrue\u90a3\u4e48\u5728express\u4e2d<br \/>\n\u9700\u8981\u8bbe\u7f6e\u201d\u4fe1\u4efb\u4ee3\u7406\u201c\u3002<\/p>\n<pre class=\"javascript hljs\" style=\"color: #000000;\"><span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">var<\/span> app = express()\r\napp.set(<span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'trust proxy'<\/span>, <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">1<\/span>) <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/ trust first proxy <\/span>\r\napp.use(session({\r\n  secret: <span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'keyboard cat'<\/span>,\r\n  resave: <span class=\"hljs-literal\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">false<\/span>,\r\n  saveUninitialized: <span class=\"hljs-literal\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">true<\/span>,\r\n  cookie: { secure: <span class=\"hljs-literal\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">true<\/span> }\r\n}))<\/pre>\n<p><span style=\"color: #555555;\">\u5982\u679c\u5728\u751f\u4ea7\u73af\u5883\u4e0b\u9700\u8981\u4f7f\u7528\u5b89\u5168\u7684cookit,\u540c\u65f6\u5728\u6d4b\u8bd5\u73af\u5883\u4e5f\u8981\u80fd\u591f\u4f7f\u7528\u3002\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528express\u4e2d\u7684NODE_ENV\u53c2\u6570\u00a0<\/span><\/p>\n<pre class=\"javascript hljs\" style=\"color: #000000;\"><span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">var<\/span> app = express()\r\n<span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">var<\/span> sess = {\r\n  secret: <span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'keyboard cat'<\/span>,\r\n  cookie: {}\r\n}\r\n<span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">if<\/span> (app.get(<span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'env'<\/span>) === <span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'production'<\/span>) {\r\n  app.set(<span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'trust proxy'<\/span>, <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">1<\/span>) <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/ trust first proxy <\/span>\r\n  sess.cookie.secure = <span class=\"hljs-literal\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">true<\/span> <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/ serve secure cookies <\/span>\r\n}\r\napp.use(session(sess))<\/pre>\n<p><span style=\"color: #555555;\">cookie\u7684secure\u5c5e\u6027\u53ef\u4ee5\u8bbe\u7f6e\u4e3aauto,\u90a3\u4e48\u4f1a\u6309\u7167\u8bf7\u6c42\u7684\u65b9\u5f0f\u6765\u5224\u65ad\uff0c\u5982\u679c\u662f\u5b89\u5168\u7684\u5c31\u662fsecure\u3002\u4f46\u662f\u5982\u679c\u7f51\u7ad9\u540c\u65f6\u652f\u6301HTTP\u548cHTTPS\uff0c\u8fd9\u65f6\u5019\u901a\u8fc7HTTPS\u8bbe\u7f6e\u7684cookie\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u5bf9\u4e8eHTTP\u662f\u4e0d\u53ef\u89c1\u7684\u3002\u8fd9\u5728express\u7684\u201dtrust proxy\u201c\uff08\u7b80\u5316\u5f00\u53d1\u548c\u751f\u4ea7\u73af\u5883\uff09\u6b63\u786e\u8bbe\u7f6e\u7684\u60c5\u51b5\u4e0b\u7279\u522b\u6709\u7528\u3002\u9ed8\u8ba4\u4e0b\uff1acookie.maxAge\u4e3anull\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u8fd9\u610f\u5473\u7740\uff0c\u6d4f\u89c8\u5668\u5173\u95ed\u4e86\u8fd9\u4e2acookie\u4e5f\u5c31\u8fc7\u671f\u4e86\u3002\u00a0<\/span><br style=\"color: #555555;\" \/><strong style=\"font-weight: bold; color: #555555;\">req.session:<\/strong><\/p>\n<pre class=\"javascript hljs\" style=\"color: #000000;\"><span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/ Use the session middleware <\/span>\r\napp.use(session({ secret: <span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'keyboard cat'<\/span>, cookie: { maxAge: <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">60000<\/span> }}))\r\n<span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/ Access the session as req.session <\/span>\r\napp.get(<span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'\/'<\/span>, <span class=\"hljs-function\" style=\"font-weight: inherit; font-style: inherit;\"><span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">function<\/span><span class=\"hljs-params\" style=\"font-weight: inherit; font-style: inherit; color: #0000ff;\">(req, res, next)<\/span> <\/span>{\r\n  <span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">var<\/span> sess = req.session<span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u7528\u8fd9\u4e2a\u5c5e\u6027\u83b7\u53d6session\u4e2d\u4fdd\u5b58\u7684\u6570\u636e\uff0c\u800c\u4e14\u8fd4\u56de\u7684JSON\u6570\u636e<\/span>\r\n  <span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">if<\/span> (sess.views) {\r\n    sess.views++\r\n    res.setHeader(<span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'Content-Type'<\/span>, <span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'text\/html'<\/span>)\r\n    res.write(<span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'&lt;p&gt;views: '<\/span> + sess.views + <span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'&lt;\/p&gt;'<\/span>)\r\n    res.write(<span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'&lt;p&gt;expires in: '<\/span> + (sess.cookie.maxAge \/ <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">1000<\/span>) + <span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'s&lt;\/p&gt;'<\/span>)\r\n    res.end()\r\n  } <span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">else<\/span> {\r\n    sess.views = <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">1<\/span>\r\n    res.end(<span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">'welcome to the session demo. refresh!'<\/span>)\r\n  }\r\n})<\/pre>\n<p><strong style=\"font-weight: bold; color: #555555;\">Session.regenerate()\uff1a<\/strong><span style=\"color: #555555;\">\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u4ea7\u751f\u4e00\u4e2asession\uff0c\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u90a3\u4e48\u4e00\u4e2a\u65b0\u7684SID\u548cSession\u5b9e\u4f8b\u5c31\u4f1a\u88ab\u521b\u5efa\uff0c\u540c\u65f6\u653e\u7f6e\u5728req.session\u4e2d\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">session.destory():\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0\u9500\u6bc1session\uff0c\u540c\u65f6\u5728req.session\u4e2d\u88ab\u79fb\u9664\uff0c\u4f46\u662f\u5728\u4e0b\u4e00\u6b21\u8bf7\u6c42\u7684\u65f6\u5019\u53c8\u4f1a\u88ab\u521b\u5efa\u00a0<\/span><\/p>\n<pre class=\"javascript hljs\" style=\"color: #000000;\">       req.session.destroy(<span class=\"hljs-function\" style=\"font-weight: inherit; font-style: inherit;\"><span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">function<\/span><span class=\"hljs-params\" style=\"font-weight: inherit; font-style: inherit; color: #0000ff;\">(err)<\/span> <\/span>{\r\n      <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/ cannot access session here <\/span>\r\n    })<\/pre>\n<p><strong style=\"font-weight: bold; color: #555555;\">session.reload():<\/strong><span style=\"color: #555555;\">\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 \u91cd\u65b0\u88c5\u8f7dsession\u4e2d\u7684\u6570\u636e\u00a0<\/span><\/p>\n<pre class=\"javascript hljs\" style=\"color: #000000;\">        req.session.reload(<span class=\"hljs-function\" style=\"font-weight: inherit; font-style: inherit;\"><span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">function<\/span><span class=\"hljs-params\" style=\"font-weight: inherit; font-style: inherit; color: #0000ff;\">(err)<\/span> <\/span>{\r\n      <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/ session updated <\/span>\r\n    })<\/pre>\n<p><strong style=\"font-weight: bold; color: #555555;\">session.save():<\/strong><span style=\"color: #555555;\">\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0\u628asession\u4e2d\u7684\u6570\u636e\u91cd\u65b0\u4fdd\u5b58\u5230store\u4e2d\uff0c\u7528\u5185\u5b58\u7684\u5185\u5145\u53bb\u66ff\u6362\u6389store\u4e2d\u7684\u5185\u5bb9\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728HTTP\u7684\u54cd\u5e94\u540e\u81ea\u52a8\u88ab\u8c03\u7528\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0\u5982\u679csession\u4e2d\u7684\u6570\u636e\u88ab\u6539\u53d8\u4e86\uff08\u8fd9\u4e2a\u884c\u4e3a\u53ef\u4ee5\u901a\u8fc7\u4e2d\u95f4\u4ef6\u7684\u5f88\u591a\u7684\u914d\u7f6e\u6765\u6539\u53d8\uff09\uff0c\u6b63\u56e0\u4e3a\u5982\u6b64\u8fd9\u4e2a\u65b9\u6cd5\u4e00\u822c\u4e0d\u7528\u663e\u793a\u8c03\u7528\u3002\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0\u4f46\u662f\u5728\u957f\u8fde\u63a5\u7684websocket\u4e2d\u8fd9\u4e2a\u65b9\u6cd5\u4e00\u822c\u9700\u8981\u624b\u52a8\u8c03\u7528\u00a0\u00a0<\/span><\/p>\n<pre class=\"javascript hljs\" style=\"color: #000000;\">     req.session.save(<span class=\"hljs-function\" style=\"font-weight: inherit; font-style: inherit;\"><span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">function<\/span><span class=\"hljs-params\" style=\"font-weight: inherit; font-style: inherit; color: #0000ff;\">(err)<\/span> <\/span>{\r\n    <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/ session saved <\/span>\r\n  })<\/pre>\n<p><strong style=\"font-weight: bold; color: #555555;\">session.touch():<\/strong><span style=\"color: #555555;\">\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0\u66f4\u65b0maxAge\u5c5e\u6027\uff0c\u4e00\u822c\u4e0d\u9700\u8981\u624b\u52a8\u8c03\u7528\uff0c\u56e0\u4e3asession\u7684\u4e2d\u95f4\u4ef6\u5df2\u7ecf\u66ff\u4f60\u8c03\u7528\u4e86\u00a0<\/span><br style=\"color: #555555;\" \/><strong style=\"font-weight: bold; color: #555555;\">req.session.id\uff1a<\/strong><span style=\"color: #555555;\">\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0\u552f\u4e00\u7684\uff0c\u800c\u4e14\u4e0d\u4f1a\u88ab\u6539\u53d8\u00a0<\/span><br style=\"color: #555555;\" \/><strong style=\"font-weight: bold; color: #555555;\">req.session.cookie\uff1a<\/strong><span style=\"color: #555555;\">\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u6bcf\u4e00\u4e2asession\u90fd\u6709\u4e00\u4e2acookie\u5bf9\u8c61\uff0c\u56e0\u6b64\u5728\u6bcf\u4e00\u6b21\u8bf7\u6c42\u7684\u65f6\u5019\u4f60\u90fd\u53ef\u4ee5\u6539\u53d8session\u7684cookie\u3002\u5982\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7req.session.cookie.expires\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0\u8bbe\u7f6e\u4e3afalse\uff0c\u8fd9\u65f6\u5019\u6d4f\u89c8\u5668\u5173\u95edcookie\u5c31\u4e0d\u5b58\u5728\u4e86\u00a0<\/span><br style=\"color: #555555;\" \/><strong style=\"font-weight: bold; color: #555555;\">Cookie.maxAge\uff1a<\/strong><span style=\"color: #555555;\">\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0req.session.cookie.maxAge\u8fd4\u56de\u8fd9\u4e2acookie\u5269\u4f59\u7684\u6beb\u79d2\u6570\uff0c\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eexpires\u6765\u5b8c\u6210\u00a0<\/span><\/p>\n<pre class=\"javascript hljs\" style=\"color: #000000;\">  <span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">var<\/span> hour = <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">3600000<\/span>\r\n    req.session.cookie.expires = <span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">new<\/span> <span class=\"hljs-built_in\" style=\"font-weight: inherit; font-style: inherit; color: #0086b3;\">Date<\/span>(<span class=\"hljs-built_in\" style=\"font-weight: inherit; font-style: inherit; color: #0086b3;\">Date<\/span>.now() + hour)\r\n    req.session.cookie.maxAge = hour<span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u548c\u4e0a\u9762\u7684expires\u7b49\u4ef7<\/span><\/pre>\n<p><span style=\"color: #555555;\">\u00a0 \u00a0 \u5f53maxAge\u8bbe\u7f6e\u4e3a60000\uff0c\u4e5f\u5c31\u662f\u4e00\u5206\u949f\uff0c\u8fd9\u65f6\u5019\u5982\u679c\u5df2\u7ecf\u8fc7\u53bb\u4e8630s\uff0c\u90a3\u4e48maxAge\u5c31\u4f1a\u8fd4\u56de30000\uff08\u4e0d\u8fc7\u8981\u7b49\u5230\u5f53\u524d\u8bf7\u6c42\u7ed3\u675f\uff09\u3002\u5982\u679c\u8fd9\u65f6\u5019\u6211\u4eec\u8c03\u7528\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u00a0 req.session.touch()\uff0c\u90a3\u4e48req.session.maxAge\u5c31\u6210\u4e86\u521d\u59cb\u503c\u4e86\u00a0<\/span><br style=\"color: #555555;\" \/><strong style=\"font-weight: bold; color: #555555;\">req.sessionID\uff1a<\/strong><span style=\"color: #555555;\">\u00a0<\/span><br style=\"color: #555555;\" \/><span style=\"color: #555555;\">\u00a0 \u53ea\u8bfb\u7684\u5c5e\u6027\u00a0<\/span><\/p>\n<p style=\"color: #555555;\">\u6bcf\u4e00\u4e2asession store\u5fc5\u987b\u662f\u4e00\u4e2aEventEmitter\u5bf9\u8c61\uff0c\u540c\u65f6\u8981\u5b9e\u73b0\u7279\u5b9a\u7684\u65b9\u6cd5\u3002required\u65b9\u6cd5\u8868\u793a\uff1a\u5728\u8fd9\u4e2astore\u4e0a\u4e00\u5b9a\u4f1a\u8c03\u7528\u7684\u65b9\u6cd5<br \/>\nRecommended\u65b9\u6cd5\u8868\u793a\u5982\u679c\u6709\u8fd9\u4e2a\u65b9\u6cd5\u90a3\u4e48\u5728\u8fd9\u4e2astore\u4e0a\u5c31\u4f1a\u8c03\u7528\u3002Optional\u65b9\u6cd5\u8868\u793a\u4e0d\u4f1a\u8c03\u7528\uff0c\u4f46\u662f\u4e3a\u4e86\u7ed9\u7528\u6237\u4e00\u4e2a\u7edf\u4e00\u7684store!<\/p>\n<p style=\"color: #555555;\"><strong style=\"font-weight: bold; font-style: inherit;\">store.destroy(sid, callback)<\/strong><br \/>\n\u5fc5\u987b\u7684\u65b9\u6cd5\u3002\u901a\u8fc7sessionID\u6765\u9500\u6bc1session\uff0c\u5982\u679csession\u5df2\u7ecf\u88ab\u9500\u6bc1\uff0c\u90a3\u4e48\u56de\u8c03\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u540c\u65f6\u4f20\u5165\u4e00\u4e2aerror\u5bf9\u8c61<br \/>\n<strong style=\"font-weight: bold; font-style: inherit;\">store.get(sid, callback)<\/strong><br \/>\n\u5fc5\u987b\u7684\u65b9\u6cd5\u3002\u901a\u8fc7sessionID\u4ecestore\u4e2d\u83b7\u53d6session\u3002\u56de\u8c03\u51fd\u6570\u662fcallback(err,session)\u3002\u5982\u679csession\u5b58\u5728\u90a3\u4e48\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5c31\u662fsession<br \/>\n\u5426\u5219\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5c31\u662fnull\/undefined\u3002\u5982\u679cerror.code===&#8221;ENOENT&#8221;\u90a3\u4e48\u56de\u8c03\u4e3acallback(null,null)<br \/>\n<strong style=\"font-weight: bold; font-style: inherit;\">store.set(sid, session, callback)<\/strong><br \/>\n\u5fc5\u987b\u7684\u65b9\u6cd5\u3002\u5982\u679c\u88ab\u6210\u529f\u8bbe\u7f6e\u4e86\u90a3\u4e48\u56de\u8c03\u4e3acallback(error)<br \/>\n<strong style=\"font-weight: bold; font-style: inherit;\">store.touch(sid, session, callback)<\/strong><br \/>\n\u63a8\u8350\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4e00\u4e2a\u6307\u5b9a\u7684sid\u548csession\u5bf9\u8c61\u53bb\u201d\u63a5\u89e6\u201c\u8fd9\u4e2asession.\u5982\u679c\u63a5\u89e6\u5230\u4e86\u90a3\u4e48\u56de\u8c03\u4e3acallback(error)\u3002session store\u7528\u8fd9\u4e2a\u65b9\u6cd5\u53bb<br \/>\n\u5220\u9664\u90a3\u4e9b\u7a7a\u95f2\u7684session\u3002\u540c\u65f6\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u4f1a\u901a\u77e5session store\u6307\u5b9a\u7684session\u662f\u6d3b\u52a8\u6001\u7684\u3002<br \/>\n<strong style=\"font-weight: bold; font-style: inherit;\">store.length(callback)<\/strong><br \/>\n\u53ef\u9009\u7684\u65b9\u6cd5\u3002\u83b7\u53d6store\u4e2d\u6240\u6709\u7684session\u7684\u4e2a\u6570\uff0c\u56de\u8c03\u51fd\u6570\u4e3acallback(error,length)<br \/>\n<strong style=\"font-weight: bold; font-style: inherit;\">store.clear(callback)<\/strong><br \/>\n\u53ef\u9009\u7684\u65b9\u6cd5\uff0c\u4ecestore\u4e2d\u5427\u6240\u6709\u7684session\u90fd\u5220\u9664\uff0c\u56de\u8c03\u51fd\u6570\u4e3acallback(err)<br \/>\n<strong style=\"font-weight: bold; font-style: inherit;\">store.all(callback)<\/strong><br \/>\n\u53ef\u9009\u7684\u65b9\u6cd5\u3002\u4ee5\u4e00\u4e2a\u6570\u7ec4\u7684\u65b9\u6cd5\u83b7\u53d6store\u4e2d\u7684sessions\u3002callback(error,sessions)<\/p>\n<pre class=\"javascript hljs\" style=\"color: #000000;\">session({\r\n    secret: settings.cookieSecret,\r\n    <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/blog=s%3AisA3_M-Vso0L_gHvUnPb8Kw9DohpCCBJ.OV7p42pL91uM3jueaJATpZdlIj%2BilgxWoD8HmBSLUSo<\/span>\r\n    <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u5176\u4e2dsecret\u5982\u679c\u662f\u4e00\u4e2astring\uff0c\u90a3\u4e48\u5c31\u662f\u7528\u8fd9\u4e2astring\u5bf9sessionID\u5bf9\u5e94\u7684cookie\u8fdb\u884c\u7b7e\u540d\uff0c\u5982\u679c\u662f\u4e00\u4e2a\u6570\u7ec4\u90a3\u4e48\u53ea\u6709\u7b2c\u4e00\u4e2a\u7528\u4e8e\u7b7e\u540d\uff0c\u5176\u4ed6\u7528\u4e8e\u6d4f\u89c8\u5668\u8bf7\u6c42\u540e\u7684\u9a8c\u8bc1<\/span>\r\n    key: settings.db,\r\n    <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u8bbe\u7f6e\u7684cookie\u7684\u540d\u5b57\uff0c\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u5230\u8fd9\u91cc\u6307\u5b9a\u7684\u662fblog\uff0c\u6240\u4ee5\u6d4f\u89c8\u5668\u7684\u8bf7\u6c42\u4e2d\u53ef\u4ee5\u770b\u5230\u8fd9\u91cc\u7684sessionID\u5df2\u7ecf\u4e0d\u662fsessionID\u4e86\uff0c\u800c\u662f\u8fd9\u91cc\u7684blog<\/span>\r\n    name:<span class=\"hljs-string\" style=\"font-weight: inherit; font-style: inherit; color: #219161;\">\"qinliang\"<\/span>,<span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/name\u7684\u4f18\u5148\u7ea7\u6bd4key\u8981\u9ad8\uff0c\u5982\u679c\u540c\u65f6\u8bbe\u7f6e\u4e86\u90a3\u4e48\u5c31\u662f\u6309\u7167name\u6765\u5236\u5b9a\u7684<\/span>\r\n    <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u6ca1\u6709name\u65f6\u5019response\u4e2d\u4e3a\uff1aset-cookie:blog=s%3A6OJEWycwVMmTGXcZqawrW0HNLOTJkYKm.0Slax72TMfW%2B4Tiit3Ox7NAj5S6rPWvMUr6sY02l0DE; Path=\/; Expires=Thu, 28 Apr 2016 10:47:13 GMT; HttpOnly<\/span>\r\n    <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u5f53\u6709name\u7684\u65f6\u5019resopnse\u4e2d\uff1aset-cookie:qinliang=s%3ABDOjujVhV0DH9Atax_gl4DgZ4-1RGvjQ.OeUddoRalzB4iSmUHcE8oMziad4Ig7jUT1REzGcYcdg; Path=\/; Expires=Thu, 28 Apr 2016 10:48:26 GMT; HttpOnly<\/span>\r\n    resave:<span class=\"hljs-literal\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">true<\/span>,<span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u6ca1\u6709\u5b9e\u73b0touch\u65b9\u6cd5\uff0c\u540c\u65f6\u4e5f\u8bbe\u7f6e\u4e86session\u7684\u8fc7\u671f\u65f6\u95f4\u4e3a30\u5929<\/span>\r\n    rolling:<span class=\"hljs-literal\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">true<\/span>,<span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u5982\u679c\u8bbe\u7f6e\u4e86rolling\u4e3atrue\uff0c\u540c\u65f6saveUninitialized\u4e3atrue\uff0c\u90a3\u4e48\u6bcf\u4e00\u4e2a\u8bf7\u6c42\u90fd\u4f1a\u53d1\u9001\u6ca1\u6709\u521d\u59cb\u5316\u7684session\uff01<\/span>\r\n    saveUninitialized:<span class=\"hljs-literal\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">false<\/span>,<span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u8bbe\u7f6e\u4e3atrue,\u5b58\u50a8\u7a7a\u95f4\u6d6a\u8d39\uff0c\u4e0d\u5141\u8bb8\u6743\u9650\u7ba1\u7406<\/span>\r\n    cookie: \r\n    {\r\n        maxAge: <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">1000<\/span> * <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">60<\/span> * <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">60<\/span> * <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">24<\/span> * <span class=\"hljs-number\" style=\"font-weight: inherit; font-style: inherit; color: #40a070;\">30<\/span>\r\n     },\r\n    <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/cookie\u91cc\u9762\u5168\u90e8\u7684\u8bbe\u7f6e\u90fd\u662f\u5bf9\u4e8esessionID\u7684\u5c5e\u6027\u7684\u8bbe\u7f6e\uff0c\u9ed8\u8ba4\u7684\u5c5e\u6027\u4e3a{ path: '\/', httpOnly: true, secure: false, maxAge: null }.<\/span>\r\n    <span class=\"hljs-comment\" style=\"font-weight: inherit; font-style: italic; color: #408080;\">\/\/\u6240\u4ee5\u6700\u540e\u6211\u4eec\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u91cc\u9762\u7684\u4fe1\u606f\u5c31\u662f\uff1a{\"cookie\":{\"originalMaxAge\":2592000000,\"expires\":\"2016-04-27T02:30:51.713Z\",\"httpOnly\":true,\"path\":\"\/\"},\"flash\":{}}<\/span>\r\n    store: <span class=\"hljs-keyword\" style=\"font-weight: inherit; font-style: inherit; color: #954121;\">new<\/span> MongoStore({\r\n      db: settings.db,\r\n      host: settings.host,\r\n      port: settings.port\r\n    })\r\n})<\/pre>\n<p><a href=\"http:\/\/www.voidcn.com\/blog\/liangklfang\/article\/p-5749242.html\" target=\"_blank\">http:\/\/www.voidcn.com\/blog\/liangklfang\/article\/p-5749242.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u7528\uff1a\u7528\u6307\u5b9a\u7684\u53c2\u6570\u521b\u5efa\u4e00\u4e2asession\u4e2d\u95f4\u4ef6\uff0csesison\u6570\u636e\u4e0d\u662f\u4fdd\u5b58\u5728cookie\u4e2d\uff0c\u4ec5\u4ec5session [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-5162","post","type-post","status-publish","format-standard","hentry","category-nodejs"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5162"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5162\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}