当前位置:网站首页>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 !
边栏推荐
- 同花顺开户注册安全靠谱吗?有没有风险的?
- MySQL——idea连接MySQL
- The post-90s resigned and started a business, saying they would kill cloud database
- 17 websites for practicing automated testing. I'm sure you'll like them
- Remember the experience of automatically jumping to spinach station when the home page was tampered with
- Redis data migration (II)
- MySQL——索引
- Capture de paquets et tri du contenu externe - - autoresponder, composer, statistiques [3]
- JS notes (III)
- Nmap and masscan have their own advantages and disadvantages. The basic commands are often mixed to increase output
猜你喜欢

(5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan

TiDB 之 TiCDC6.0 初体验

No more! Technical team members resign collectively
![[vulnhub shooting range] impulse: lupinone](/img/27/b92eeceefd1c71f19d926bdd1eee8b.jpg)
[vulnhub shooting range] impulse: lupinone

Basic preprocessing and data enhancement of image data

Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files

Collections SQL communes

MySQL——索引

What is the maximum number of concurrent TCP connections for a server? 65535?

Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
随机推荐
Experience summary of database storage selection
MySQL——数据库备份
技术管理进阶——如何在面试中考察候选人并增大入职概率
gslb(global server load balance)技术的一点理解
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!...
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
Selenium has three waiting methods (forced waiting, implicit waiting, and display waiting)
Etcd 基于Raft的一致性保证
Décompiler et modifier un exe ou une DLL non source en utilisant dnspy
Goodbye 2021, how do programmers go to the top of the disdain chain?
Qualcomm platform WiFi update disconnect end open event
Baohong industry | good habits that Internet finance needs to develop
鹏城杯 WEB_WP
@Scenario of transactional annotation invalidation
The White House held an open source security summit, attended by many technology giants
Last week's content review
Rhcsa third day notes
Kubernetes 通信异常网络故障 解决思路
Kernel symbol table
抓包整理外篇——————autoResponder、composer 、statistics [ 三]