Node. JS (vi)--http Concept Advanced

Source: Internet
Author: User

In the above steps, the HTTP request is swept to the end of a closed loop including the request header information, The response time the server returned header information, status code, etc.

These are available in the node HTTP module and have the appropriate interface to assemble the information and return them, while the HTTP interfaces are designed to support many features that support the HTTP protocol

These features are difficult to use in traditional development, such as Large-byte block-coded messages

To allow the user to manipulate the data interface in the form of requests and responses to cache the entire request or response, the full set of HTTP APIs is very low, it only handles operations between streams and information parsing

The information can be parsed into the request header and the request body, but not to parse the actual request header and the content inside the request body

This is the introduction of the HTTP module in Nodejs


What is a callback?

Callback is the most basic method of asynchronous programming, for nodejs, the need to execute asynchronous logic in order, generally follow the way, that is, the subsequent logic encapsulated in the callback function as the starting function of the parameters, layered nesting in this way to let the program in the way we want to go through the process

The cases are as follows:

Declaring a Learn function

function Learn (something) {console.log (something)}

And then we declare a we function, two parameters

The first one is the callback callback, easy to understand

function we (callback,something) {something + = ' is cool ' callback (something)}

The last to invoke

We (learn, ' Nodejs ')

The results of the implementation are as Follows:

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M02/8F/0E/wKioL1jSb2PittXKAAAOjAoJcxo205.jpg "title=" 36020170315204858576.jpg "alt=" wkiol1jsb2pittxkaaaojaojcxo205.jpg "/>

This is called a named function (a function with a Name)

There is also an anonymous function that is called directly as Follows:

We (function (something) {console.log (something)}, ' Jade ')

The results of the implementation are as Follows:

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M00/8F/10/wKiom1jSb8rDh7huAAARrKIR-qc195.jpg "title=" 36020170315204858576.jpg "alt=" wkiom1jsb8rdh7huaaarrkir-qc195.jpg "/>



What is synchronous/asynchronous?

For asynchronous opposites is synchronization, synchronization is the execution of a task, the latter task waits for the previous task to end before Executing.

The execution order of the program is consistent with the order of the task, the browser side of JS is single-threaded, so the JS code can only be executed sequentially, even if the JS can be parallel to download, but also can only be executed sequentially,

If there is a more time-consuming operation in a js, then the page will be blocked, the subsequent JS code will not be executed to

For example, a page with 3 JS introduced

<script src= ' a.js ' ></script>//<script src= ' b.js ' ></script>//<script src= ' c.js ' > The </script>b.js code is as follows var i = 0while (true) {i++}

This JS will be blocked here all the Time.

The asynchronous pattern is completely different.

Each task has one or more callback functions, and the previous task does not perform the latter task after the end, but instead executes the callback function, and the latter task does not wait for the previous task to finish executing the task execution order is inconsistent with the order, is asynchronous

The most basic asynchronous functions in JavaScript are settimeout and Setinterval.

SetTimeout executes the given function at the end of a given time, accepting a callback function as the first argument

The code is as follows--sequential execution (first edition):

var c = 0function printit () {console.log (c)}function plus () {c +=1}plus () printit ()

The result is undoubtedly: 1

The results are as follows:

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M01/8F/10/wKiom1jSc8Ki5X3kAAAIyH2WZzA748.jpg "title=" 36020170315204858576.jpg "alt=" wkiom1jsc8ki5x3kaaaiyh2wzza748.jpg "/>

If there are some asynchronous operations in plus or more time-consuming operations, then this is not necessarily

Can get the result that we expect, add a settimeout, then execute, then can get what result?

The code is as follows--blocking execution (second edition):

var c = 0function printit () {console.log (c)}function plus () {setTimeout (function () {c +=1},1000)}plus () printit ()

The results of the implementation are as Follows:

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M02/8F/0E/wKioL1jSdKqxywnmAAAI1r7Y2Aw396.jpg "title=" 36020170315204858576.jpg "alt=" wkiol1jsdkqxywnmaaai1r7y2aw396.jpg "/>

That's not what we expected. 1


What about this time?

You can change the plus method, pass a callback function, and then call the callback function in the Settimeout.

Then pass the result, and finally pass the PrintIt as a parameter to the plus

The code is as follows--sequential execution (first edition):

var c = 0function printit () {console.log (c)}function plus (callback) {setTimeout (function () {c +=1callback (c)},1000)} Plus (printit)

The results of the implementation are as Follows:

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M02/8F/11/wKiom1jSdZvz5e0wAAAH8CHpP3w912.jpg "title=" 36020170315204858576.jpg "alt=" wkiom1jsdzvz5e0waaah8chpp3w912.jpg "/>

finally, We print out the results we expected: 1.


What is i/o?

The disk writes and reads in the nodejs, itself is provides the interface to the file system, the database and so on resources,

When sending a request to the file system, do not wait for the hard disk, and when the hard disk is ready, the Non-blocking interface will notify the Nodejs


What is single thread/multithreading?

A single thread corresponds to multithreading

Single-threaded: the functions of the program are executed sequentially, and only the previous process is executed successfully,

The subsequent process can be executed to

Single Thread features: secure

Each has advantages and disadvantages, multithreading is to grasp the resource allocation


What is blocking/non-blocking?



What is an event?

What is event-driven?

What is an event-driven callback?

What is an event loop?



This article is from the "it rookie" blog, make sure to keep this source http://mazongfei.blog.51cto.com/3174958/1909380

Node. JS (vi)--http Concept Advanced

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.