关于Stream流

文章目录

Stream流

什么是流? Node中为什么要有流这个概念? 使用场景?

流指的是数据流,指的是数据是分片传输的

gulp 【流试操作】

案例

const fs = require(‘fs’)

引入读取文件

const zlib =require(‘zlib’)

引入压缩包

const inp =fs.createReadStream(‘路径’)

读取数据

const gzip = zlib.createGzip()

创建压缩包

const outp = fs.createWriteStream(‘输出路径’)

inp

.pipe(gizp)

.pipe(outp)

输出压缩包

第三方模块

我们一般可以从npmjs.com这个网站拉取

使用流程:

1.安装

先创建package.json文件 (npm init -y)
下载request第三方插件(npm i request -D)

2.使用

request这个模块是做数据请求的

3.Node 中数据请求不存在跨域问题(因为Node是可以脱离浏览器的)

案例

const request = require(‘request’)

request(‘https://m.lagou.com/listmore.json’,( a,b,c ) => {

console.log( ‘a’,a ) // error

console.log( ‘b’,b ) // response 返回所有结果

console.log( ‘c’,c ) // body 数据 string

})

fs

Node.js读取文件都是二进制流

Buffer

binary

const fs = require( ‘fs’ )

fs.readFile(‘文件名’,( error, docs ) => {

console.log( docs.toString() )

})

fs.readFile(‘文件名’,‘utf8’,( error, docs ) => {

console.log( docs.toString() )

})

toString 和 utf8 都可以解码

搭建简单服务器爬虫

后端服务器有两种类型

1.web服务器 【静态服务器】

2.api服务器 【暴露接口】

请求头部的报文

1.general 请求基本信息

2.response Headers 响应头

3. request Headers 请求头

4.携带参数

- query string paramters get请求
- from data post请求

const http = require( ‘http’ )
const host = ‘localhost’
const port = 8000

http.createServer(( request,response ) => {

// response.writeHead( 状态码,请求头 )
response.writeHead( 200, {
‘Content-type’: ‘text/html;charset=utf8’
})

const http = require( 'http' )
// 引入第三方类库
const cheerio = require( 'cheerio' )

const options = {
  hostname: 'jx.1000phone.net',
  port: 80,
  path: '/teacher.php/Class/classDetail/param/rqiWlsefmajGmqJhXXWhl3ZiZGZp',
  method: 'GET',
  headers: {
    Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'Cache-Control':' no-cache',
    Cookie: 'PHPSESSID=ST-159231-9hv0Ja04QabYv-n5OYwWsQFVfjw-izm5ejd5j1npj2pjc7i3v4z',
    Host: 'jx.1000phone.net',
    Pragma: 'no-cache',
    'Proxy-Connection': 'keep-alive',
    Referer: 'http://jx.1000phone.net/teacher.php/Class/index',
    'Upgrade-Insecure-Requests': 1,
    'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': 0
  }
}


http.get( options, (res) => {


  const { statusCode } = res; // 获取请求状态码  200 301 302 303 304 404  500 
  const contentType = res.headers['content-type']; // 获取请求类型数据类型

  // json数据的content-type         'content-type': 'application/json'


  res.setEncoding('utf8'); // 字符编码
  let rawData = '';
  res.on('data', (chunk) => { rawData += chunk; }); // 通过data事件拼接数据流
  res.on('end', () => { // 获取数据结束了 
    try { // 高级编程语法         捕获错误信息
      // console.log( rawData )
      const $ = cheerio.load( rawData )
      $('td.student a').each(function (index,item) {
        response.write(`<h3> ${ $( this ).text() }  </h3>`) // 往前端发送信息
      })
      response.end() // 告诉前端信息已经结束了
    } catch (e) {
      console.error(e.message);
    }
  });
}).on('error', (e) => {
  console.error(`Got error: ${e.message}`);
});

}).listen( port,host,() => {
console.log( The server is running at: http://${ host }:${ port } )
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值