当前位置:网站首页>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 !
边栏推荐
- Etcd raft Based Consistency assurance
- Qualcomm platform WiFi -- P2P issue
- Inventory 2021 | yunyuansheng embracing the road
- 使用dnSpy对无源码EXE或DLL进行反编译并且修改
- MySQL——JDBC
- What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
- MySQL - idea connects to MySQL
- [gd32l233c-start] 5. FLASH read / write - use internal flash to store data
- 2022-02-15 Daily: 2022 AAAI fellow release
- Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
猜你喜欢

Compilation Principle -- syntax analysis

Advanced collaboration: coroutinecontext

QFileDialog

What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics

Study diary: February 14th, 2022

运维各常用命令总结

Single page application architecture

MySQL——数据库备份

Yiwen teaches you how to choose your own NFT trading market

一台服务器最大并发 tcp 连接数多少?65535?
随机推荐
Common SQL sets
Memory analyzer (MAT)
Goodbye 2021, how do programmers go to the top of the disdain chain?
Selenium has three waiting methods (forced waiting, implicit waiting, and display waiting)
JS three families
MySQL - SQL injection problem
What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
(5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan
全网都在疯传的《老板管理手册》(转)
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
鹏城杯 WEB_WP
Study diary: February 14th, 2022
The post-90s resigned and started a business, saying they would kill cloud database
常用sql集合
Transformation between yaml, Jason and Dict
A little understanding of GSLB (global server load balance) technology
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
Great gods, I want to send two broadcast streams: 1. Load basic data from MySQL and 2. Load changes in basic data from Kafka
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks
XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向