{"id":195,"date":"2021-02-10T10:08:47","date_gmt":"2021-02-10T02:08:47","guid":{"rendered":"http:\/\/gjweb.top\/?p=195"},"modified":"2021-02-10T10:08:47","modified_gmt":"2021-02-10T02:08:47","slug":"session%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=195","title":{"rendered":"session\u8bbe\u7f6e"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e0b\u8f7d<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">npm install express-session --save \/\/\u7528\u4e8e\u5728express\u4e2d\u64cd\u4f5csession<br>npm install connect-mongo --save \/\/ \u7528\u4e8e\u5c06session\u5199\u5165\u6570\u636e\u5e93(session\u6301\u4e45\u5316)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5f15\u5165<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ \u8fdb\u5165express-session\u6a21\u5757<br>const session = require('express-session')<br>\/\/ \u5f15\u5165connect-mongo\u6a21\u5757<br>const MongoStore = require(\"connect-mongo\")(session);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7f16\u5199\u5168\u5c40\u914d\u7f6e\u5bf9\u8c61<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">app.use(session({<br> &nbsp; &nbsp;name:'userid' \/\/ \u8bbe\u7f6ecookie\u7684name,\u9ed8\u8ba4\u503c\u662fconnect.sid<br> &nbsp; &nbsp;secret:'dataKey', \/\/ \u53c2\u4e0e\u52a0\u5bc6\u7684\u5b57\u7b26\u4e32(\u53c8\u79f0\u4e3a\u7b7e\u540d)<br> &nbsp; &nbsp;saveuninitialized:false, \/\/ \u662f\u5426\u5728\u5b58\u50a8\u5185\u5bb9\u4e4b\u524d\u521b\u5efa\u4f1a\u8bdd<br> &nbsp; &nbsp;resave:true,\/\/\u662f\u5426\u5728\u6bcf\u6b21\u8bf7\u6c42\u65f6,\u5f3a\u5236\u91cd\u65b0\u4fdd\u5b58session,\u5373\u4f7f\u4ed6\u4eec\u6ca1\u6709\u53d8\u5316<br> &nbsp; &nbsp;store:new MongoStore({<br> &nbsp;      url:'mongodb:\/\/localhost:27017\/sessions_container',<br> &nbsp;      touchAfter:24*3600 \/\/\u4fee\u6539\u9891\u7387(\/\/\u572824\u5c0f\u65f6\u4e4b\u5185\u66f4\u65b0\u4e00\u6b21)    <br>    }),<br> &nbsp; &nbsp;cookie:{<br> &nbsp;      httpOnly:true, \/\/ \u5f00\u542f\u540e\u524d\u7aef\u65e0\u6cd5\u901a\u8fc7JS\u64cd\u4f5ccookie<br> &nbsp; &nbsp; &nbsp; &nbsp;maxAge:1000*30 \/\/ \u8bbe\u7f6ecookie\u7684\u8fc7\u671f\u65f6\u95f4<br> &nbsp;  }<br>}))<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5411session\u4e2d\u6dfb\u52a0\u4e00\u4e2axxx,\u503c\u4e3ayyy<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">req.session.xxx = yyy<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u83b7\u53d6session\u4e0a\u7684xxx\u5c5e\u6027<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">const {xxx} = req.session<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6574\u4e2a\u8fc7\u7a0b\u662f:<br>    1.\u5ba2\u6237\u7aef\u7b2c\u4e00\u6b21\u53d1\u8d77\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u5f00\u542f\u4e00\u4e2asession\u4e13\u95e8\u7528\u4e8e\u5b58\u50a8\u8fd9\u6b21\u8bf7\u6c42\u7684\u4e00\u4e9b\u4fe1\u606f\u3002<br> &nbsp; &nbsp;2.\u6839\u636e\u914d\u7f6e\u5bf9\u8c61\u7684\u4fe1\u606f\uff0c\u670d\u52a1\u5668\u51b3\u5b9a\u662f\u5426\u8fdb\u884c\uff1asession\u6301\u4e45\u5316\u7b49\u5176\u4ed6\u64cd\u4f5c\u3002<br> &nbsp; &nbsp;2.\u4e0e\u6b64\u540c\u65f6\u670d\u52a1\u5668\u521b\u5efa\u4e86\u4e00\u4e2acookie\uff0c\u5b83\u7684key\u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u6307\u5b9a\uff0c\u4f46\u662f\u5b83\u7684value\u4e00\u5b9a\u662f\u4e0a\u4e00\u6b65session\u7684\u552f\u4e00\u6807\u8bc6\u3002<br> &nbsp; &nbsp;3.\u670d\u52a1\u5668\u5c06\u6211\u4eec\u6307\u5b9a\u597d\u7684\u5185\u5bb9\u6dfb\u52a0\u8fdbsession\u5bf9\u8c61\uff0c\u4f8b\u5982\uff1areq.session.xxxx = yyy\u3002<br> &nbsp; &nbsp;4.\u7b49\u8bf7\u6c42\u518d\u6b21\u8fc7\u6765\u65f6\uff0c\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u4e2d\u5305\u542b\u7740\u4e4b\u524d\u201c\u79cd\u201d\u7684cookie\u3002<br> &nbsp; &nbsp;5.\u670d\u52a1\u5668\u68c0\u67e5\u643a\u5e26\u8fc7\u6765\u7684cookie\u662f\u5426\u6709\u6548\uff0c\u51b3\u5b9a\u662f\u5426\u53bb\u8bfb\u53d6\u5bf9\u5e94session\u4e2d\u7684\u4fe1\u606f\u3002<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u8f7d npm install express-session &#8211;save \/\/\u7528\u4e8e\u5728express\u4e2d\u64cd\u4f5cse [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-express"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=195"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}