成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

網(wǎng)絡操作 回調函數(shù) http

0xE7A38A / 3196人閱讀

摘要:作為客戶端使用的時候,發(fā)起客戶端請求,用來獲得服務器端的響應服務器端的是以事件作為驅動的,創(chuàng)建服務器時的回調函數(shù)就會被調用一次,即,這是事件驅動請求頭的請求本質是數(shù)據(jù)流,由請求頭和請求體組成。

網(wǎng)絡操作 首先使用http模塊實現(xiàn)一個http服務器
var http = require("http");    // 使用http模塊

http.createServer (
        function (request, response) {
            response.writeHead(200, {"Content-Type": "text-plain"});    // http響應頭部
            response.end("hello word
");    // 返回的內容
        }
    ).listen(8124);    // 監(jiān)聽8124端口
PS C:UsersmingmDesktop	est> node main.js

訪問http://127.0.0.1:8124/ 返回hello word

一些api http模塊

兩種方式,

作為服務器端使用的時,創(chuàng)建一個http服務器,監(jiān)聽http客戶端請求,并返回響應。

作為客戶端使用的時候,發(fā)起http客戶端請求,用來獲得服務器端的響應

服務器端的是以事件作為驅動的,創(chuàng)建服務器時的回調函數(shù)就會被調用一次,即,這是事件驅動

http請求頭

http的請求本質是數(shù)據(jù)流,由請求頭和請求體組成。
打開瀏覽器的開發(fā)者工具,選擇network面板,然后,刷新頁面,再次,選擇一個文件,在headers窗口中,顯示出當前文件請求的http頭部信息

先是請求頭,后是請求體
http請求發(fā)送給服務器時,是從頭到尾一個一個字節(jié)以數(shù)據(jù)流的方式發(fā)送,http模塊創(chuàng)建的http服務器在接收到完整的請求頭以后,進行回調函數(shù),

var http = require("http");    // 使用http模塊

http.createServer (
        function (request, response) {
            var body = [];

            console.log(request.method);
            console.log("--------------");
            console.log(request.headers);
            console.log("---------------");
        }
    ).listen(8124);    // 監(jiān)聽8124端口
PS C:UsersmingmDesktop	est> node main.js
GET
--------------
{ host: "127.0.0.1:8124",
  connection: "keep-alive",
  "cache-control": "max-age=0",
  "upgrade-insecure-requests": "1",
  dnt: "1",
  "user-agent":
   "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
  accept:
   "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
  "accept-encoding": "gzip, deflate, br",
  "accept-language": "zh-CN,zh;q=0.9" }
---------------
回調函數(shù)
var fs = require("fs");

fs.readFile("input.txt", function (err, data) {
    console.log("3333");
    console.log(err);
    console.log(data.toString());
    console.log("3333");
});

console.log("程序執(zhí)行結束!");
PS C:UsersmingmDesktop	est> node main.js
程序執(zhí)行結束!
3333
null
33333333333333333333333333
3333
PS C:UsersmingmDesktop	est>

當遇到需要i/o操作的時候,先跳過執(zhí)行,在執(zhí)行當前的內容。所以結果為此,然后在將執(zhí)行完成的結果傳給參數(shù)列表的最后一個函數(shù),所以最后一個函數(shù)為回調

http的回調函數(shù),請求
var http = require("http");

http.createServer(
    function (request, response) {
        var body = [];

        console.log(request.method);
        console.log(request.headers);

    console.log(1111111111);
    console.log(body);

       request.on("end", function () {
        body = Buffer.concat(body);
        console.log(222222222222222);
        console.log(body.toString());
    });

    console.log(4444444444444);
    response.writeHead(200, {"Content-Type": "text-plain"});
    response.end("hello word
");
    console.log(55555555555);
    }
).listen(8124);

執(zhí)行結果

PS C:UsersmingmDesktop	est> node main.js
GET
{ host: "127.0.0.1:8124",
  connection: "keep-alive",
  "cache-control": "max-age=0",
  "upgrade-insecure-requests": "1",
  "user-agent":
   "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
  dnt: "1",
  accept:
   "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
  "accept-encoding": "gzip, deflate, br",
  "accept-language": "zh-CN,zh;q=0.9" }
1111111111
[]
4444444444444
55555555555
222222222222222

GET
{ host: "127.0.0.1:8124",
  connection: "keep-alive",
  pragma: "no-cache",
  "cache-control": "no-cache",
  "user-agent":
   "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
  dnt: "1",
  accept: "image/webp,image/apng,image/*,*/*;q=0.8",
  referer: "http://127.0.0.1:8124/",
  "accept-encoding": "gzip, deflate, br",
  "accept-language": "zh-CN,zh;q=0.9" }
1111111111
[]
4444444444444
55555555555
222222222222222

此執(zhí)行為異步執(zhí)行,先執(zhí)行到

console.log(body);

由于request.on需要等待返回,所以異步執(zhí)行下方的語句

console.log(444);

接著返回內容,再執(zhí)行request.on,將結果通知回到函數(shù)的最后一個參數(shù),然后執(zhí)行完畢。

http響應

服務端原樣將客戶端請求的請求體,返回給客戶端

PS C:UsersmingmDesktop	est> node main.js
444444444444
22222222
33333333
555555
var http = require("http");

http.createServer(function (request, response){
    console.log(444444444444);
    response.writeHead(200, { "Content-Type": "text/plain" });

                    // 為響應頭,即原路發(fā)送給客戶端
                    request.on(
                        "data", 
                        function (chunk) {
                            response.write(chunk);
                            console.log(111111);
                    });

                    console.log(22222222);
                    request.on("end", function() {response.end();console.log(555555)});
                    console.log(33333333);
                }
).listen(8124);

寫的有點亂

http客戶端

node發(fā)送一個http客戶端請求

var options = {
    hostname: "www.iming.info",
    port: 80,    // 端口為80
    path: "/upload",    // 請求的路徑
    method: "POST",        // 請求的方法為post方法
    headers: {
        "Content-Type": "application/x-www-form-urlencoded"    // 頭部信息
    },
}

var http = require("http");

var request = http.request(options, function (response) {});

request.write("hello word!");
request.end();

以上發(fā)送了一個http請求。
下面是node.js的事件循環(huán)
貌似明白一點api的原理了。

文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://m.hztianpu.com/yun/108083.html

相關文章

  • 后端知識點總結——NODE.JS(高級)

    摘要:階段是事件循環(huán)的第一階段習慣上往往都會設置數(shù)將回調函數(shù)添加到事件循環(huán)的階段的隊列中等待執(zhí)行。 后端知識點總結——NODE.JS(高級) 1.Node入門: 什么是: 針對網(wǎng)絡應用開發(fā)的平臺主要特征: 基于Google的JavaScript運行時引擎V8 擴展了Node標準類庫: TCP,同步或異步文件管理,HTTP 為什么使用Node: 可以在服務器端運行js: 現(xiàn)有前端團隊可直...

    bovenson 評論0 收藏0
  • 全面分析前端的網(wǎng)絡請求方式

    摘要:請求默認會攜帶同源請求的,而跨域請求則不會攜帶,設置的的屬性為將允許攜帶跨域。類型請求成功后的回調函數(shù)。另外,同樣提供了在環(huán)境下的支持,可謂是網(wǎng)絡請求的首選方案。當網(wǎng)絡故障時或請求被阻止時,才會標記為,如跨域不存在,網(wǎng)絡異常等會觸發(fā)。 一、前端進行網(wǎng)絡請求的關注點 大多數(shù)情況下,在前端發(fā)起一個網(wǎng)絡請求我們只需關注下面幾點: 傳入基本參數(shù)(url,請求方式) 請求參數(shù)、請求參數(shù)類型 設...

    Edison 評論0 收藏0
  • Express 實戰(zhàn)(二):Node.js 基礎

    摘要:而通過實現(xiàn)名為的標準模塊,完美的解決了模塊導入問題。通常都被稱為包管理器,而這也是它最大的特色。例如,接受請求發(fā)送響應。該模塊主要處理文件相關內容,其中大多數(shù)都是文件讀寫功能。 在上一篇文章中,我們簡單的介紹了 Node.js 。了解到它基于 JavaScript、天生異步、擁有大量的第三方類庫。本文將會在之前的基礎上,對 Node.js 進行更深入的介紹。其中主要內容包括: Nod...

    soasme 評論0 收藏0
  • 以圖表和示例的角度解讀async/await

    摘要:在中,表示抽象的非阻塞異步執(zhí)行。在完成之后安排代碼的唯一方式是通過方法綁定回調函數(shù)。下圖描述了該示例的計算過程方法中綁定的回調函數(shù)只有當成功的時候才會調用。為了處理失敗的,需要通過綁定另一個回調函數(shù)。 介紹 ES7中,async/await 語法使異步promise的協(xié)調變得很簡單。如果你需要以特定順序異步獲取來自多個數(shù)據(jù)庫或API的數(shù)據(jù),可以使用雜亂的promise或回調函數(shù)。asy...

    sutaking 評論0 收藏0
  • 總結:JavaScript異步、事件循環(huán)與消息隊列、微任務與宏任務

    摘要:單線程異步非阻塞然后,這又牽扯到了事件循環(huán)消息隊列,還有微任務宏任務這些。此步的位置不確定某個時刻后,定時器觸發(fā)線程通知事件觸發(fā)線程,事件觸發(fā)線程將回調函數(shù)加入消息隊列隊尾,等待引擎線程執(zhí)行。 前言 Philip Roberts 在演講 great talk at JSConf on the event loop 中說:要是用一句話來形容 JavaScript,我可能會這樣: Java...

    qianfeng 評論0 收藏0

發(fā)表評論

0條評論

0xE7A38A

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<