javascript node.js 中的 request 和 http 模块有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27783806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:59:27  来源:igfitidea点击:

What is the difference between request and http modules in node.js?

javascript

提问by Jamsheed Kamarudeen

My task is a very simple one, send an http/https request to a server, get back the HTML,JSON or XML and process the data.

我的任务很简单,向服务器发送一个 http/https 请求,取回 HTML、JSON 或 XML 并处理数据。

I understand that there are 2 modules that can do the module part. nodejs.org/api/http.html and https://www.npmjs.com/package/request

我知道有 2 个模块可以完成模块部分。nodejs.org/api/http.html 和 https://www.npmjs.com/package/request

I guess request is more advanced. Other than that is there any difference between the 2 which makes one more suitable or less suitable for the task I said?

我想请求更高级。除此之外,这两个之间有什么区别,哪个更适合或更不适合我说的任务?

回答by Aaron Digulla

The httppackage contains support for the raw HTTP protocol. While it can do everything, often it's a bit clumsy to use.

http包包含对原始 HTTP 协议的支持。虽然它可以做任何事情,但使用起来通常有点笨拙。

The requestmodule uses the httpmodule and adds a lot of sugar to make it easier to digest: A lot of common cases can be handled with just a tiny bit of code, it supports piping request data, forwarding requests to a different server, etc.

request模块使用该http模块并添加了大量糖以使其更易于消化:只需一点点代码即可处理很多常见情况,它支持管道请求数据,将请求转发到不同的服务器等。