Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js res.end()和res.send()之间有什么区别?_Node.js_Express - Fatal编程技术网

Node.js res.end()和res.send()之间有什么区别?

Node.js res.end()和res.send()之间有什么区别?,node.js,express,Node.js,Express,我是Express.js的初学者,我被这两个关键词弄糊涂了:res.end()和res.send() 它们是相同的还是不同的?res.send()将发送HTTP响应。它的语法是 res.send([body]) body参数可以是缓冲区对象、字符串、对象或数组。例如: res.send(new Buffer('whoop')); res.send({ some: 'json' }); res.send('<p>some html</p>'); res.status(40

我是
Express.js
的初学者,我被这两个关键词弄糊涂了:
res.end()
res.send()

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

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

res.send([body])
body参数可以是缓冲区对象、字符串、对象或数组。例如:

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' });
res.end();
res.status(404).end();

阅读了解更多信息。

我想强调一下
res.end()
res.send()
在响应头方面的一些关键区别,以及它们的重要性

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


其中,使用res.end()时,您只能用文本响应,而不会设置“内容类型”

app.get('/',(req,res)=>{
res.end(“你好”);
}); 

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

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

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

res.end()

  • 它检查您发送的数据并设置正确的响应头
  • 然后用
    res.write
    对数据进行流式处理
  • 最后,它使用
    res.end
    设置请求的结束

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

    res
    是一个从OutgoingMessage扩展而来的HttpResponse对象
    res.send
    调用
    res.end
    ,通过OutgoingMessage发送HTTP响应并关闭连接来实现。我们看到code

    res.send
    用于向客户端发送响应,其中
    res.end
    用于结束您发送的响应


    res.send
    自动调用
    res.end
    ,这样您就不必在
    res.send

    之后调用或提及它。除了这些出色的答案之外,我想在这里强调何时使用res.end()以及何时使用res.send()。这就是我最初登陆这里并没有找到解决方案的原因

    答案很简单

    res.end()用于在不发送任何数据的情况下快速结束响应

    例如,在服务器上启动一个进程

    app.get(/start-service, (req, res) => {
       // Some logic here
       exec('./application'); // dummy code
       res.end();
    });
    
    如果要在响应中发送数据,则应使用res.send()代替

    在这里你可以阅读更多


    但是res.end实际上可以起到res.send的作用,因为您可以传递一个字符串参数作为响应主体进行添加。除此之外,res.send还将结束响应。那么它们在功能上有什么不同呢?@psytech140 Jmar77有一个很好的响应:“如果将字符串传递给res.send(),它会自动假定内容类型为html.res.end(),但是,它只是在响应流上调用节点的底层end()实现,因此不会假定内容类型。”我不得不对此进行投票,纯粹是因为我从未使用过
    express
    ,但从标题思想来看——一个人发送一些东西,一个人结束一些东西TWA是对的。但是如果您只使用
    res.send()
    ,而不使用任何内容呢。这是否类似于
    res.end()
    ?我认为普通节点使用的
    res.end
    的区别,其中as
    res.send
    由express framwork
    response使用。send
    包括
    response.end
    调用。我认为这是可以接受的答案。。。更多的讨论是关于可能破坏你工作日的实际差异,而不是讨论结束有/没有内容的回复…对这些伟大的信息投一票!!!真棒的回答,谢谢!
         app.get('/',(req,res)=>{
             res.send({msg:'hello'});
         });
    
          app.get('/',(req,res)=>{
               res.end('<b>hello</b>');
          }); 
    
          app.get('/',(req,res)=>{
                res.send('<b>hello</b>');
          });
    
    app.get(/start-service, (req, res) => {
       // Some logic here
       exec('./application'); // dummy code
       res.end();
    });
    
    app.get(/start-service, (req, res) => {
       res.send('{"age":22}');
    });