res.end() 和 res.send() 有什么区别? [英] What is the difference between res.end() and res.send()?

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

问题描述

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

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

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

Are they the same or different?

推荐答案

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

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

res.send([body])

body 参数可以是 Buffer 对象、String、对象或 Array.例如:

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

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 this for more info.

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

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

阅读了解更多信息.

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

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