美文网首页
核心模块——http

核心模块——http

作者: 小弋呀 | 来源:发表于2016-07-22 01:09 被阅读612次

http模块API内容也不是很多,主要有http.server、http.ClientRequest、http.ServerResponse、http.IncomingMessage四个类

一、http.server

服务器类,创建一个服务器对象的方法如下:

const http = require('http');
const server = http.createServer((req, res) => { 
  //req即是http.IncomingMessage类
  //res是http.ServerResponse类
 res.end();
});
server.listen(8000);

这就创建了一个服务器,监听8000端口,127.0.0.1:8000,createServer方法的回调函数用于监听server的request事件

二、http.IncomingMessage

上例中的req继承自此类,主要用于获取请求信息,属性如下:

request

现在自己用到了标出的这几个,
req.url获取链接中的path部分,搭配URL模块可以轻松获取get请求的查询字符串

三、http.ServerResponse

例子中的res,用于响应客户端请求

response
var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, { 'Content-Type': 'text-plain' });
    response.write('Hello ');
    response.end('World\n');
}).listen(8000);
四、http.ClientRequest

这是我理解为后端代理,用于在服务器发送请求,可以用这个写个小爬虫程序

  • http.get(options[, callback])
    发送get请求,不需要有请求体
http.get('http://www.google.com/index.html', (res) => { 
  console.log(`Got response: ${res.statusCode}`); // consume  response body
  res.resume();
})
.on('error', (e) => { 
  console.log(`Got error: ${e.message}`);
});

此处的res是用于接收另一个服务端的响应,而不是去响应,可以堪称这个时候有点类似http.IncomingMessage(当然这完全不是一回事)

  • http.request(options[, callback])
    用于发送post请求,由于post请求需要有请求体,参数就复杂多了,官网的例子:
var postData = querystring.stringify({ 'msg' : 'Hello World!'});
var options = { 
    hostname: 'www.google.com', port: 80, 
    path: '/upload', 
    method: 'POST', 
    headers: { 
        'Content-Type': 'application/x-www-form-urlencoded', 
        'Content-Length': Buffer.byteLength(postData) }
 };
var req = http.request(options, (res) => {
     console.log(`STATUS: ${res.statusCode}`); 
     console.log(`HEADERS: ${JSON.stringify(res.headers)}`); 
     res.setEncoding('utf8'); 
     res.on('data', (chunk) => {
       console.log(`BODY: ${chunk}`); 
     }); 
     res.on('end', () => {
       console.log('No more data in response.') 
     })
});
req.on('error', (e) => { 
    console.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(postData);
req.end();

相关文章

  • 核心模块——http

    http模块API内容也不是很多,主要有http.server、http.ClientRequest、http.S...

  • HTTP

    HTTP模块 第一个板块:简单介绍HTTP模块用法 //第一步:引入模块http模块是核心模块 var http=...

  • Node 源码 —— http 模块

    Node 源码 —— http 模块 http 模块位于 /lib/http.js,我们直接看该模块的核心方法 c...

  • nginx 2

    1 nginx 常用模块整理 nginx 常用模块整理1 http核心模块 ngx_http_core_modu...

  • Node.js 之 http模块

    http模块 引入http模块 开启一个本地服务器需要Node.js中http核心模块http--模块提供了搭建本...

  • nginx 常用配置记录

    HTTP模块(核心模块,也是主要用到的模块) server模块 server模块是http的子模块,它用来定义一个...

  • 那些年成为node攻城狮的路(二)

    模块加载机制浅析 node中模块分为核心模块和文件模块两大类,核心模块诸如fs、http、util...,文件模块...

  • Node学习随笔—http相关__http模块&路由

    一:http模块 http模块是node的常用模块,可以用浏览器访问写的代码 1.引进http模块(核心模块不需要...

  • Node.js(七)HTTP模块http

    http是Node.js的核心模块之一,要使用http模块,需要:const http = require('ht...

  • Nginx 系列二:Nginx 服务器架构初探

    一、Nginx模块化结构 习惯上将 Nginx 涉及到的模块分为核心模块、标准 HTTP 模块、可选 HTTP 模...

网友评论

      本文标题:核心模块——http

      本文链接:https://www.haomeiwen.com/subject/kbxijttx.html