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版)
Cookie
和
Session
|
97
我们仔细区分了输入错误和数据库错误。要记住,即使已经在前端做了输入验证(前端是
应该做的),在后端还是要做,因为恶意用户是可以绕过前端验证的。
flash
消息是网站可选择的
、非常好的机制,尽管在某些情景中会有更合适的方法(例如,
对于多表单“向导”或购物车结账流程,
flash
消息往往不合适)
。在开发期间,
flash
消息
也很有用,因为它是一种提供反馈的简便方法,即使以后你要把它替换为别的技术。在搭
建一个网站时,增加
flash
消息的支持是我最先做的事情之一。在本书后面的章节中,
也会
使用这项技术。
因为
flash
消息是在中间件中从
Session
传到
res.locals.flash
的,你需要
做一次重定向
flash
消息才能显示
。如果你想不经重定向就显示,直接设置
res.locals.flash
,而不是
req.session.flash
。
本章的示例使用浏览器提交表单并重定向,因为像这样使用
Session
来控制
UI
的做法
,通常是不会用在由
Ajax
提交表单的应用中的。在
Ajax
提交表
单的应用中,你会希望错误信息能够在后端表单处理返回的
JSON
中体现出
来,然后前端修改
DOM
以动态地显示错误信息
。这并不是说
Session
不适
用于前端渲染的应用,而是说
Session
很少这样使用。
9.6
 
Session
的用途
当你想跨越多个页面保存用户的偏好设置时,
Session
就很有用了。最常见的是使用
Session
来提供用户认证信息
:当你登录的时候,一个
Session
就创建了。之后,每次你重
新加载页面都不需要再次登录 ...
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