当前位置:网站首页>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.Use
Ofresponse.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 !
边栏推荐
- 抓包整理外篇——————autoResponder、composer 、statistics [ 三]
- Kubernetes abnormal communication network fault solution ideas
- QFileDialog
- Etcd raft Based Consistency assurance
- 浅析 Ref-NeRF
- How to choose cache read / write strategies in different business scenarios?
- QT6 QML book/qt quick 3d/ Basics
- TiDB 之 TiCDC6.0 初体验
- Selenium has three waiting methods (forced waiting, implicit waiting, and display waiting)
- 电子科技大学|强化学习中有效利用的聚类经验回放
猜你喜欢
Single page application architecture
Rhcsa third day operation
Persistence of Nacos
TiDB 之 TiCDC6.0 初体验
Monkey/ auto traverse test, integrate screen recording requirements
Nacos common configuration
Tidb's initial experience of ticdc6.0
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
[gd32l233c-start] 5. FLASH read / write - use internal flash to store data
设计电商秒杀系统
随机推荐
Getting started with postman -- environment variables and global variables
XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向
What if the Flink SQL client exits and the table is emptied?
MySQL——数据库备份
What is the maximum number of concurrent TCP connections for a server? 65535?
内存分析器 (MAT)
UI automation test: selenium+po mode +pytest+allure integration
Advanced collaboration: coroutinecontext
MySQL - SQL injection problem
@Scenario of transactional annotation invalidation
Volley source code analysis
Nacos common configuration
For in, foreach, for of
Design e-commerce seckill system
Kubernetes 通信异常网络故障 解决思路
(5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan
Persistence of Nacos
Set, weakset, map, weakmap in ES6
Quickly distinguish slices and arrays
抓包整理外篇——————autoResponder、composer 、statistics [ 三]