国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JavaScript > 正文

js中回調函數實現一個http服務器

2020-03-22 20:24:47
字體:
來源:轉載
供稿:網友
本篇文章分享給大家的內容是關于js中回調函數實現一個http服務器,內容很詳細,接下來我們就來看看具體的內容,希望可以幫助到大家。

網絡操作首先使用http模塊實現一個http服務器
var http = require( http // 使用http模塊http.createServer ( function (request, response) { response.writeHead(200, { Content-Type : text-plain // http響應頭部 response.end( hello word/n // 返回的內容 ).listen(8124); // 監聽8124端口
PS C:/Users/mingm/Desktop/test node main.js

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

一些apihttp模塊

兩種方式,

作為服務器端使用的時,創建一個http服務器,監聽http客戶端請求,并返回響應。

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

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

http請求頭

http的請求本質是數據流,由請求頭和請求體組成。
打開瀏覽器的開發者工具,選擇network面板,然后,刷新頁面,再次,選擇一個文件,在headers窗口中,顯示出當前文件請求的http頭部信息
img src= https://melovemingming-125387...;
同樣,火狐的也一樣
img src= https://melovemingming-125387...;
先是請求頭,后是請求體
http請求發送給服務器時,是從頭到尾一個一個字節以數據流的方式發送,http模塊創建的http服務器在接收到完整的請求頭以后,進行回調函數,

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); // 監聽8124端口
PS C:/Users/mingm/Desktop/test node main.js--------------{ 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 }---------------
回調函數
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( 程序執行結束! 
PS C:/Users/mingm/Desktop/test node main.js程序執行結束!33333333333333333333333333PS C:/Users/mingm/Desktop/test 

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

http的回調函數,請求
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/n  console.log(55555555555);).listen(8124);

執行結果

PS C:/Users/mingm/Desktop/test node main.js{ 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 }1111111111444444444444455555555555222222222222222{ 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 }1111111111444444444444455555555555222222222222222

此執行為異步執行,先執行到

console.log(body);

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

console.log(444);

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

http響應

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

PS C:/Users/mingm/Desktop/test node main.js4444444444442222222233333333555555
var http = require( http http.createServer(function (request, response){ console.log(444444444444); response.writeHead(200, { Content-Type : text/plain }); // 為響應頭,即原路發送給客戶端 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發送一個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();

以上發送了一個http請求。

相關推薦:

axios源碼解析如何實現一個HTTP請求庫

以上就是js中回調函數實現一個http服務器的詳細內容,PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大足县| 成都市| 拉萨市| 贵阳市| 双牌县| 乌苏市| 陇西县| 南安市| 峡江县| 巴马| 金平| 灌阳县| 从化市| 沂水县| 新化县| 莱西市| 略阳县| 工布江达县| 新宁县| 建宁县| 潮州市| 阿荣旗| 石狮市| 阜城县| 班戈县| 浪卡子县| 黄龙县| 宣汉县| 雷波县| 木里| 旬阳县| 沽源县| 石泉县| 潍坊市| 广水市| 上蔡县| 岑巩县| 阿坝县| 肥西县| 嘉峪关市| 淮滨县|