The HTTP module of the node. JS Module

Source: Internet
Author: User
Tags http post

Node is also a good choice if you want to initiate an HTTP connection to a remote server. Node is ideal for use in many scenarios, such as using Web service, connecting to a document database, or crawling a Web page. You can use the same HTTP module to initiate an HTTP request, but you should use HTTP. Clientrequest class. The class has two factory methods: a common approach and a convenient method.

var http = require (' http ');
var opts = {
Host: ' Www.google.com '

PORT:80,
Path: '/',
Method: ' GET '
};
var req = http.request (opts, function (res) {
Console.log (RES);
Res.on (' Data ', function (data) {
Console.log (data);
});
});
Req.end ();

(method) is optional and, if not specified, is set to get by default. The last thing to note is that you need to end (end ()) the request. Because this is a GET request, we do not send any data to the server . But for other HTTP methods, such as put or post, you may need to send data. Request waits for the end () method call before initializing the HTTP request because, until then, it is not sure whether we will still send the data.

GET is a common way to use HTTP, so it provides a specialized factory method to make it easier to use
It, as shown in example 4-10.

var http = require (' http ');
var opts = {
Host: ' Www.google.com '
PORT:80,
Path: '/',
};

var req = Http.get (opts, function (res) {
Console.log (RES);
Res.on (' Data ', function (data) {
Console.log (data);
});
});

The example of Http.get () and the previous example did the same thing, but more clearly. We removed the method attribute from the configuration object and also removed the Request.end (), as these are already implicitly explained. If you run these two examples, the results you get will be the bare data of the buffer object. Later in this chapter, Buffer is a specially defined class of node that is used to support the storage of any binary data. Although you can also use this content directly, you typically specify the encoding, such as UTF-8 (the encoding format of a Unicode character), which can be specified by the Response.setencoding () method.

> var http = require (' http ');
> var req = http.get ({host: ' www.google.com ', port:80, Path: '/'},
Function (res) {
... console.log (res);
... res.on (' Data ', function (c) {console.log (c);});
... });
> <buffer 3c 6f 63 74 79 70
...
2e 74>
<buffer 61 72 74 54 69
...
The 3e>
>
> var req = http.get ({host: ' www.google.com ', port:80, Path: '/'},
Function (res) {
... res.setencoding (' UTF8 ');
... res.on (' Data ', function (c) {console.log (c);});
... });
> <!doctype html>Req.write ("My Data");
Req.write ("More of my Data");
Req.end ();
This example is similar to example 4-10, but adds HTTP. Clientrequest.write () method. You can use this method to send upstream data streams. As explained earlier, it requires you to explicitly invoke HTTP. The Clientrequest.end () method indicates that the data has been sent. Whenever Clientrequest.write () is called, the data is immediately uploaded (not cached), but the server will not respond to your data request before the Clientrequest.end () call. You can bind the data event of a stream with clientrequest.write () so that it can be sent to the server in the form of a stream. This can be a good idea, for example, when you need to send a file on your hard disk over HTTP to a remote server.

The HTTP module of the node. JS Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.