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版)
表单处理
|
85
res.send({ result: 'success' })
},
}
可以在表单处理函数里做任何需要的操作,通常是把数据保存到数据库。如果存在问题,
就发送一个包含
err
属性的
JSON
对象(而不是
result:
success
)。
在这个示例中,假设所有的
Ajax
请求都预期
JSON
响应,但是并不存在
Ajax
必须要用
JSON
通信这样的要求
(事实上,
Ajax
曾经是首字母缩写,
其中“
X
”代表了
“
XML
”)。 使 用
JSON
通信对
JavaScript
十分友好,因为
JavaScript
十分擅长处理
JSON
。如果你要开放普遍可以访问的
Ajax
端
点,
或者你知道
Ajax
请求可能使用
JSON
以外的格式
,就应该
完全
基于
Accepts
请求头来返回适当的响应(
Accepts
请求头可以方便地通过
req.accepts
来访
问)。如果仅基于
Accepts
请求头来做响应,那么或许应该看看
res.format
,
它是一个简便方法,让你可以轻松地根据客户端的预期发回适当的响应。如
果是那样,当你用
JavaScript
发起
Ajax
请求时
,就要确保设置了
dataType
或
accepts
属性。
8.7
 文件上传
我们已经提到过,文件上传会使复杂性剧增。幸运的是,借助一些非常优秀的项目,文件
上传就成了小事一桩。
对于多部分表单的处理,有
4
个既流行又健壮的项目
:
busboy
、
multiparty
、
formidable
和
multer
。这
4
个我都用过
,都很不错,但我感觉
multiparty
是维护得最好的,因此这里就使 ...
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