当前位置:网站首页>What is the difference between res.send() and res.end() in the node express framework
What is the difference between res.send() and res.end() in the node express framework
2022-07-03 21:35:00 【Yisu cloud】
node Express In the framework res.send() and res.end() What's the difference?
This article mainly explains “node Express In the framework res.send() and res.end() What's the difference? ”, Interested friends might as well come and have a look . The method introduced in this paper is simple and fast , Practical . Now let Xiaobian take you to learn “node Express In the framework res.send() and res.end() What's the difference? ” Well !

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 .
Here we are , I'm sure you're right “node Express In the framework res.send() and res.end() What's the difference? ” Have a deeper understanding of , You might as well put it into practice ! This is the Yisu cloud website , For more relevant contents, you can enter the relevant channels for inquiry , Pay attention to our , Continue to learn !
边栏推荐
- Why use pycharm to run the use case successfully but cannot exit?
- Hcie security Day10: six experiments to understand VRRP and reliability
- Global and Chinese market of telematics boxes 2022-2028: Research Report on technology, participants, trends, market size and share
- XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向
- [gd32l233c-start] 5. FLASH read / write - use internal flash to store data
- No matter how hot the metauniverse is, it cannot be separated from data
- JS notes (III)
- Analyse de REF nerf
- Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
- Global and Chinese market of recycled yarn 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

treevalue——Master Nested Data Like Tensor
![Capturing and sorting out external articles -- autoresponder, composer, statistics [III]](/img/bf/ac3ba04c48e80b2d4f9c13894a4984.png)
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]

Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy

leetcode-540. A single element in an ordered array

"Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media

Memory analyzer (MAT)

Leetcode daily question 540 A single element in an ordered array Valentine's Day special article looking for a single dog in a pile of lovers ~ the clown is myself

Nacos common configuration

Imitation Netease cloud music applet

不同业务场景该如何选择缓存的读写策略?
随机推荐
Basic number theory -- Chinese remainder theorem
The 12th Blue Bridge Cup
MySQL——JDBC
Teach you how to install aidlux (1 installation)
Imitation Netease cloud music applet
使用dnSpy对无源码EXE或DLL进行反编译并且修改
设计电商秒杀系统
Brief analysis of ref nerf
Nacos common configuration
[Yugong series] go teaching course 002 go language environment installation in July 2022
(5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan
For in, foreach, for of
Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
Yiwen teaches you how to choose your own NFT trading market
鹏城杯 WEB_WP
Memory analyzer (MAT)
MySQL——索引
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
How to choose cache read / write strategies in different business scenarios?
Advanced collaboration: coroutinecontext