当前位置:网站首页>Know res.send() and res.end() of Express
Know res.send() and res.end() of Express
2022-06-23 03:18:00 【Programming samadhi】
Preface
In the use of Node.js In the server code of , If you are using Express frame , So for a request , There are often two ways to respond :
// Method 1
app.get("/end", (req, res, next) =>{
res.end(xxx);
});
// Method 2
app.get("/send", (req, res, next) =>{
res.send(xxx);
});So what's the difference between the two methods ? What are their application scenarios ? This is what I need to make clear today .
Express And res.end()
Definition
It can quickly end the response without any data .
This method actually comes from Node The core , Specifically http.ServerResponse.Use Of response.end() Method :
grammar
res.end([data[, encoding]][, callback])
Argument parsing :
- data <string> | <Buffer>
- encoding <string>
- callback <Function>
thorough
If res.end() Method passes in an object , There will be an error :
Express And res.send()
Definition
Send... To the requesting client HTTP The response message .
grammar
res.send([body[,statusCode]])
body Parameters can be Buffer、Object、String、Boolean or Array.
thorough
Through code debugging , We can find out ,Express Of res.send() The method finally calls http.ServerResponse.Use Of response.end() Method :
// node_modules/express/lib/response.js
res.send = function send(body) {
var chunk = body;
var encoding;
……
if (req.method === 'HEAD') {
// skip body for HEAD
this.end();
} else {
// respond
this.end(chunk, encoding);
}
return this;
};contrast
The same thing
Express Of res.end() and res.send() Similarities of methods :
- Both eventually return to
http.ServerResponse.UseOfresponse.end()Method . - Both will end the current response process .
Difference
Express Of res.end() and res.send() The difference in approach :
- The former can only send string perhaps Buffer type , The latter can send any type of data .
- Semantically speaking , The former is more suitable for scenarios without any response data , The latter is more suitable for scenarios with response data .
summary
Express Of res.end() and res.send() Method usage , It is generally recommended to use res.send() The method can , This eliminates the need to care about the format of the response data , because Express The data is processed internally .
~ The end of this paper , Thank you for reading !
Learn interesting knowledge , Make interesting friends , Create interesting souls !
Hello everyone , I am a 〖 The samadhi of programming 〗 The author of Hermit King
You come to , With expectations , I have ink to greet ! You go back , No matter gain or loss , Only with lingering charm !
Knowledge and skills are equally important , Both internal and external skills , We should grasp both theory and practice 、 Hard on both hands !
边栏推荐
- Centos7.6 installing mysqlclient error reporting solution
- Wi Fi 6 is coming - larger capacity, lower latency, faster network speed and more security
- How to gracefully solve the problem of platform font adaptation
- January 31, 2022: Maze III. There is a ball in the maze of open spaces and walls. ball
- Using promise to process asynchronous operations
- Interrupt array Foreach method [js implementation]
- What if the software gets stuck and cannot end the process?
- How to generate code-11 barcode in batch through TXT file
- What are the advantages of the completely free and open source flutter? How to learn about flutter?
- Cve-2021-4034 reappearance
猜你喜欢
![Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]](/img/28/ebfc25ec288627706e15a07e6bdb77.jpg)
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]
![Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]](/img/de/d73805aaf4345ca3d2a7baf85aab8d.jpg)
Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]

Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e

Encryption related to returnee of national market supervision public service platform
![Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]](/img/ca/672bfe49c8123da8679b2abeb43a2e.jpg)
Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]
![Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]](/img/84/192d152ceb760264b6b555b321f129.jpg)
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]
![Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]](/img/4b/dd272f98b89a157180bf68570d2763.jpg)
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]

Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo
随机推荐
[SaaS examination certification] apaas_ Tencent Qianfan magic pen
Quickly grab the red envelope cover of Tencent blue whale New Year! Slow hands!
Related concepts of TTF, TOF, woff and woff2
Learning records - things inherited by subclass parent of C #
2022-01-25: serialize and deserialize n-ary tree. Serialization means that a
Weekly Postgres world news 2022w04
Reading redis source code (II) underlying data structure
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]
"Tencent conference": how to operate and maintain efficiently in the face of exponential growth of business?
Copy system disk
Learning record -- superficial understanding of unity decoupling
The metauniverse is just a cloak for future technological evolution
[Alibaba middleware technology series] "Nacos technology" service registration and discovery related principle analysis
Using promise to process asynchronous operations
Use ES6 new Target to simulate abstract classes
Tachometer script
Uploading logs using loghub log4j appender
2D visual empowerment smart water green intensive development
Interrupt array Foreach method [js implementation]
CFS After the CHM file is opened, the hyperlink content cannot be loaded and blank is displayed