Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
94
|
第
9
章
9.4.1
 内存存储
如果你更愿意把
Session
信息存储到服务器上(这也是我推荐的),就需要一个存储它的地
方。入门级的选择就是内存存储。内存存储的
Session
很容易建立起来
,但有一个很大的
不足:只要重启服务器(在学习本书的过程中你会重启很多次的!),
Session
信息就会消
失。甚至还有更糟的,如果你的服务横向扩展到多台服务器上(参见第
12
章)
,每次请求
都可能由不同的服务器来提供服务,那么结果是
Session
数据有时候存在
,有时候却不存
在。这显然是用户不能接受的使用体验。不过就开发和测试来说,内存存储的
Session
已
经可以满足需要了。在第
13
章将看到如何持久化
Session
的存储信息。
首先,安装
express-session
(
npm install express-session
)。然后,在链入
Cookie
解析
器后链入
express-session
(版本库的
ch09/meadowalrk.js
):
const expressSession = require('express-session')
// 要确保在链入Session中间件之前链入了Cookie中间件!
app.use(expressSession({
resave: false,
saveUninitialized: false,
secret: credentials.cookieSecret,
}))
express-session
中间件接收一个配置对象,这个配置对象包含以下选项。
resave ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099