node.js res.end() 和 res.send() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29555290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:38:27  来源:igfitidea点击:

What is the difference between res.end() and res.send()?

node.jsexpress

提问by sudheeshcm

I'm a beginner in Express.jsand I'm confused by these two keywords: res.end()and res.send().

我是初学者,Express.js我对这两个关键字感到困惑:res.end()res.send()

Are they the same or different?

它们是相同的还是不同的?

采纳答案by itzmebibin

res.send()will send the HTTP response. Its syntax is,

res.send()将发送 HTTP 响应。它的语法是,

res.send([body])

The body parameter can be a Buffer object, a String, an object, or an Array. For example:

body 参数可以是一个 Buffer 对象、一个字符串、一个对象或一个数组。例如:

res.send(new Buffer('whoop'));
res.send({ some: 'json' });
res.send('<p>some html</p>');
res.status(404).send('Sorry, we cannot find that!');
res.status(500).send({ error: 'something blew up' });

See thisfor more info.

有关更多信息,请参阅内容。

res.end()will end the response process. This method actually comes from Node core, specifically the response.end()method of http.ServerResponse. It is used to quickly end the response without any data. For example:

res.end()将结束响应过程。这个方法其实来自于Node核心,特别response.end()http.ServerResponse. 它用于在没有任何数据的情况下快速结束响应。例如:

res.end();
res.status(404).end();

Read thisfor more info.

阅读本文了解更多信息。

回答by Jorge González

I would like to make a little bit more emphasis on some key differences between res.end()& res.send()with respect to response headers and why they are important.

我想稍微强调一下res.end()&之间res.send()在响应标头方面的一些关键差异以及它们为何重要。

1. res.send() will check the structure of your output and set header information accordingly.

1. res.send() 将检查您的输出结构并相应地设置标题信息。



    app.get('/',(req,res)=>{
       res.send('<b>hello</b>');
    });

enter image description here

在此处输入图片说明



     app.get('/',(req,res)=>{
         res.send({msg:'hello'});
     });

enter image description here

在此处输入图片说明

Where with res.end() you can only respond with text and it will not set "Content-Type"

在 res.end() 中,您只能使用文本进行响应,并且不会设置“ Content-Type

      app.get('/',(req,res)=>{
           res.end('<b>hello</b>');
      }); 

enter image description here

在此处输入图片说明

2. res.send() will set "ETag" attribute in the response header

2. res.send() 将在响应头中设置“ETag”属性

      app.get('/',(req,res)=>{
            res.send('<b>hello</b>');
      });

enter image description here

在此处输入图片说明

?Why is this tag important?
The ETag HTTP response header is an identifier for a specific version of a resource. It allows caches to be more efficient, and saves bandwidth, as a web server does not need to send a full response if the content has not changed.

?为什么这个标签很重要?
ETag HTTP 响应头是资源特定版本的标识符。它允许缓存更高效,并节省带宽,因为如果内容没有改变,Web 服务器不需要发送完整的响应。

res.end()will NOT set this header attribute

res.end()不会设置此标头属性

回答by Amit Wagner

what res.send() does, is to implement res.write,res.setHeadersand res.end.
it check's what the data you send and set the correct headers,

res.send() 的作用是实现res.writeres.setHeadersres.end
它检查您发送的数据并设置正确的标题,

then its stream the data with res.write, and in the end it uses res.end to set the end of the request.

然后它使用 res.write 传输数据,最后它使用 res.end 设置请求的结束。

there are some cases that you will want to do it manually for example if you wanna stream file or a large data set , in this cases you will want to set the headers by yours self and use the res.write in order to keep the stream flow.

在某些情况下,您需要手动执行此操作,例如,如果您想流式传输文件或大型数据集,在这种情况下,您需要自己设置标题并使用 res.write 以保持流流动。