Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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.

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

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?

like image 374
Jamsheed Kamarudeen Avatar asked Jan 05 '15 16:01

Jamsheed Kamarudeen


People also ask

What is HTTP module in node JS?

Node. js has a built-in module called HTTP, which allows Node. js to transfer data over the Hyper Text Transfer Protocol (HTTP). To include the HTTP module, use the require() method: var http = require('http');

What are the different types of HTTP requests in node JS?

http module The HTTP options specify the headers, destination address, and request method type. Next, we use http. request to send the data to the server and await the response. The response is stored in the req variable, and upon error, it is logged into the console.

What is difference between HTTP and express in node JS?

HTTP is an independent module. Express is made on top of the HTTP module. HTTP module provides various tools (functions) to do things for networking like making a server, client, etc. Express along with what HTTP does provide many more functions in order to make development easy.


1 Answers

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

The request module uses the http module 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.

like image 100
Aaron Digulla Avatar answered Oct 14 '22 04:10

Aaron Digulla