资讯专栏INFORMATION COLUMN

网络操作 回调函数 http

0xE7A38A / 3011人阅读

摘要:作为客户端使用的时候,发起客户端请求,用来获得服务器端的响应服务器端的是以事件作为驱动的,创建服务器时的回调函数就会被调用一次,即,这是事件驱动请求头的请求本质是数据流,由请求头和请求体组成。

网络操作 首先使用http模块实现一个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);    // 监听8124端口
PS C:UsersmingmDesktop	est> node main.js

访问http://127.0.0.1:8124/ 返回hello word

一些api http模块

两种方式,

作为服务器端使用的时,创建一个http服务器,监听http客户端请求,并返回响应。

作为客户端使用的时候,发起http客户端请求,用来获得服务器端的响应

服务器端的是以事件作为驱动的,创建服务器时的回调函数就会被调用一次,即,这是事件驱动

http请求头

http的请求本质是数据流,由请求头和请求体组成。
打开浏览器的开发者工具,选择network面板,然后,刷新页面,再次,选择一个文件,在headers窗口中,显示出当前文件请求的http头部信息

先是请求头,后是请求体
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: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" }
---------------
回调函数
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:UsersmingmDesktop	est> node main.js
程序执行结束!
3333
null
33333333333333333333333333
3333
PS C:UsersmingmDesktop	est>

当遇到需要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
");
    console.log(55555555555);
    }
).listen(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",
  "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

此执行为异步执行,先执行到

console.log(body);

由于request.on需要等待返回,所以异步执行下方的语句

console.log(444);

接着返回内容,再执行request.on,将结果通知回到函数的最后一个参数,然后执行完毕。

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" });

                    // 为响应头,即原路发送给客户端
                    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请求。
下面是node.js的事件循环
貌似明白一点api的原理了。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/108083.html

相关文章

  • 后端知识点总结——NODE.JS(高级)

    摘要:阶段是事件循环的第一阶段习惯上往往都会设置数将回调函数添加到事件循环的阶段的队列中等待执行。 后端知识点总结——NODE.JS(高级) 1.Node入门: 什么是: 针对网络应用开发的平台主要特征: 基于Google的JavaScript运行时引擎V8 扩展了Node标准类库: TCP,同步或异步文件管理,HTTP 为什么使用Node: 可以在服务器端运行js: 现有前端团队可直...

    bovenson 评论0 收藏0
  • 全面分析前端的网络请求方式

    摘要:请求默认会携带同源请求的,而跨域请求则不会携带,设置的的属性为将允许携带跨域。类型请求成功后的回调函数。另外,同样提供了在环境下的支持,可谓是网络请求的首选方案。当网络故障时或请求被阻止时,才会标记为,如跨域不存在,网络异常等会触发。 一、前端进行网络请求的关注点 大多数情况下,在前端发起一个网络请求我们只需关注下面几点: 传入基本参数(url,请求方式) 请求参数、请求参数类型 设...

    Edison 评论0 收藏0
  • Express 实战(二):Node.js 基础

    摘要:而通过实现名为的标准模块,完美的解决了模块导入问题。通常都被称为包管理器,而这也是它最大的特色。例如,接受请求发送响应。该模块主要处理文件相关内容,其中大多数都是文件读写功能。 在上一篇文章中,我们简单的介绍了 Node.js 。了解到它基于 JavaScript、天生异步、拥有大量的第三方类库。本文将会在之前的基础上,对 Node.js 进行更深入的介绍。其中主要内容包括: Nod...

    soasme 评论0 收藏0
  • 以图表和示例的角度解读async/await

    摘要:在中,表示抽象的非阻塞异步执行。在完成之后安排代码的唯一方式是通过方法绑定回调函数。下图描述了该示例的计算过程方法中绑定的回调函数只有当成功的时候才会调用。为了处理失败的,需要通过绑定另一个回调函数。 介绍 ES7中,async/await 语法使异步promise的协调变得很简单。如果你需要以特定顺序异步获取来自多个数据库或API的数据,可以使用杂乱的promise或回调函数。asy...

    sutaking 评论0 收藏0
  • 总结:JavaScript异步、事件循环与消息队列、微任务与宏任务

    摘要:单线程异步非阻塞然后,这又牵扯到了事件循环消息队列,还有微任务宏任务这些。此步的位置不确定某个时刻后,定时器触发线程通知事件触发线程,事件触发线程将回调函数加入消息队列队尾,等待引擎线程执行。 前言 Philip Roberts 在演讲 great talk at JSConf on the event loop 中说:要是用一句话来形容 JavaScript,我可能会这样: Java...

    qianfeng 评论0 收藏0

发表评论

0条评论

0xE7A38A

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<