What is the difference between res.send and res.write in express?

前端 未结 4 488
挽巷
挽巷 2020-12-04 19:27

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

4条回答
  •  误落风尘
    2020-12-04 19:47

    One of the most important differences not indicated in any of the answers are "draining".

    The res.write may return true or false. As of the documentation:

    Returns true if the entire data was flushed successfully to the kernel buffer. Returns false if all or part of the data was queued in user memory. 'drain' will be emitted when the buffer is free again.

    So, when doing res.write, the caller should hold off writing until the drain event emits if the res.write returned false.

    All these are handled automatically in res.send. The trade off is the buffering you will have to do when using the latter.

提交回复
热议问题