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

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

问题描述

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

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 仅在 Express.js 中.
  • 为简单的非流式响应执行许多有用的任务.
  • 能够自动分配 Content-Length HTTP 响应标头字段.
  • 能够提供自动 HEAD &HTTP 缓存新鲜度支持.
  • 实用说明
    • res.send 只能调用一次,因为它等价于 res.write + res.end()
    • 示例:
    • 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');
      });
      

    • 欲知更多详情:

      res.write

      • 可以多次调用以提供身体的连续部分.
      • 示例:
      response.write('<html>');
      response.write('<body>');
      response.write('<h1>Hello, World!</h1>');
      response.write('</body>');
      response.write('</html>');
      response.end();
      

      欲知更多详情:

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

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