express中的res.send和res.write有什么区别? [英] What is the difference between res.send and res.write in express?

查看:1350
本文介绍了express中的res.send和res.write有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是express.js的初学者,我想了解res.sendres.write之间的区别?

解决方案

重新发送

  • res.send仅在Express js中.
  • 执行许多有用的任务,以实现简单的非流式响应.
  • 能够自动分配Content-Length HTTP响应标头字段.
  • 能够提供自动HEAD& HTTP缓存新鲜度支持.
  • 实用说明

    • res.send只能被调用一次,因为它等效于res.write + res.end()
    • 示例

      app.get('/user/:id', function (req, res) {
          res.send('OK');
      });
      

了解更多详情 expressjs.com/en/api.html


res.write

  • 可以被多次调用以提供身体的连续部分.
  • 示例

    response.write('<html>');
    response.write('<body>');
    response.write('<h1>Hello, World!</h1>');
    response.write('</body>');
    response.write('</html>');
    response.end();
    

更多详细信息
nodejs.org/docs
nodejs. org/en/docs/guides

I am a beginner to express.js and I am trying to understand the difference between res.send and res.write ?

解决方案

res.send

  • res.send is only in Express js.
  • Performs many useful tasks for simple non-streaming responses.
  • Ability to automatically assigns the Content-Length HTTP response header field.
  • Ability to provides automatic HEAD & HTTP cache freshness support.
  • Practical explanation

    • res.send can only be called once, since it is equivalent to res.write + res.end()
    • Example

      app.get('/user/:id', function (req, res) {
          res.send('OK');
      });
      

for more details expressjs.com/en/api.html


res.write

  • Can be called multiple times to provide successive parts of the body.
  • Example

    response.write('<html>');
    response.write('<body>');
    response.write('<h1>Hello, World!</h1>');
    response.write('</body>');
    response.write('</html>');
    response.end();
    

For more details
nodejs.org/docs
nodejs.org/en/docs/guides

这篇关于express中的res.send和res.write有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆