image.png

模块

image.png

引用模块

  1. const os = require('os')

自己定义的模块,路径要准确,没有路径的话会优先是查找内置模块,然后再查找这个项目下已安装的模块,再查找自己定义的模块,这样就浪费时间。

API文档

====================

第三方模块

可以通过npm 包管理工具安装,可以先在上面搜索有没有完成了我需求的包,直接拿来引用。一般都会把代码放到GitHub上面

1、自动创建package.json

  1. > npm init

在项目文件夹内打开命令行,运行上面命令,就会在项目内自动创建一个包配置文件package.json
image.png

2、安装需要的包

  1. > npm install 包的名字 --save

在项目文件夹内打开命令行,运行上面命令,就会在这个项目内安装这个包

同时package.json文件内就会多出这个包的名字,如下图的“request”包
image.png

并且会在目录生成一个node_models文件夹,存放这些包和这些包依赖的包的文件。

==================

导出模块

一个js文件就是模块,可以导出这个模块的功能,给其他文件引用。

  1. module.exports.导出的名字 = 导出的变量;

module.exports是一个对象,当然是可以设置属性,导出就是一个多属性的对象。

===========================

模块初始化

image.png
引入模块,被引入的模块只会执行1次,反复require是不会反复执行的。

==================

常用内置模块

url 解析地址

  1. const url = require('url'); //引入内置模块
  2. url.parse('https://www.baidu.com') //解析链接,已弃用
  3. //解析结果
  4. Url {
  5. protocol: 'https:',
  6. slashes: true,
  7. auth: null,
  8. host: 'www.baidu.com',
  9. port: null,
  10. hostname: 'www.baidu.com',
  11. hash: null,
  12. search: null,
  13. query: null,
  14. pathname: '/',
  15. path: '/',
  16. href: 'https://www.baidu.com/'}

url.format(URL[, options]) 按格式输出

  1. const myURL = new URL('https://a:b@測試?abc#foo');
  2. console.log(myURL.href);
  3. // 打印 https://a:b@xn--g6w251d/?abc#foo
  4. console.log(myURL.toString());
  5. // 打印 https://a:b@xn--g6w251d/?abc#foo
  6. console.log(url.format(myURL, { fragment: false, unicode: true, auth: false }));,已弃用
  7. // 打印 'https://測試/?abc'

tree 生成文件树目录

image.png

querystring 查询字符串

http://nodejs.cn/api/querystring.html

解码,也就是文本变对象,参数参考api文档
querystring.parse(str[, sep[, eq[, options]]])

querystring.decode()

  1. //引入模块
  2. const querystring = require('querystring');
  3. //解码
  4. let testobj = querystring.parse('foo=bar&abc=xyz&abc=123')
  5. //{ foo: 'bar', abc: [ 'xyz', '123' ] }

编码,对象变文本,参数参考api文档
querystring.stringify(obj[, sep[, eq[, options]]])

querystring.encode()

  1. //引入模块
  2. const querystring = require('querystring');
  3. //编码
  4. let test = querystring.encode(
  5. {
  6. id:123,
  7. name:'yjl',
  8. sex:'man'
  9. }
  10. )
  11. //id=123&name=yjl&sex=man

querystring.escape(str)是转换成%编码
querystring.unescape(str)是把%编码转成文本

http / https 获取网页

get方法,配合其他模块cheerio(类似于jQuery),爬虫
https://www.bilibili.com/video/BV14E411W7hr?p=7
但是get不能获取异步的信息

  1. const http = require('http');
  2. const https = require('https');
  3. let url = ""
  4. https.get(url,function(res){
  5. var html = ''
  6. var data = ''
  7. res.on('data',function(data){
  8. html += data;
  9. })
  10. res.on('end',function(){
  11. console.log(html);
  12. })
  13. })

request 和get类似,但是参数是对象,不像get只是一个url
http://nodejs.cn/api/https.html#https_https_request_url_options_callback

也可以类似post,传递参数提交,要headers的信息

event 事件

可以自己定义事件的名字,如下图的“play”,可以用play这个名同时定义多个不同的事件执行,然后用.emit( ) 方法提交后,所有的事件会同时执行。
image.png

path 路径

解析后缀名
image.png
拼接路径
image.png
image.png
内置路径
image.png
以对象的方式解析路径
image.png
image.png

fs 文件系统

filesystem,对服务器文件进行操作

操作要注意,加上sync的是同步,没有回调函数的。
没有加的都是异步,都是有回调函数,告诉你处理完成,把你想要的结果通过回调函数拿
image.png
删除目录rmdir
删除文件unlink
image.png

image.png
image.png
image.png

异步,如果要保证执行顺序,可以用promise API,如下的async 和 await
image.png

buffer 缓冲

image.png

image.png
image.png

stream 文件流

读取、写入大文件,fs会崩溃
image.png
image.png image.png

又读又写,可以用管道流pipe
image.png

zlib 压缩

image.png
下面是链式调用,先压缩再传送

readline 输入输出

image.png

os 操作系统

axios 请求

http://www.axios-js.com/