NodeJs 插件拓展

# node-scheduleNodejs定时任务

node-schedule (opens new window) Nodejs定时任务

安装

yarn add node-schedule -D
1

使用

# Cron风格定时器

const scheduleCronstyle = () => {
  schedule.scheduleJob('1-59 * * * * *', () => {
    // sendEmail();
    console.log(11);
  });
};

scheduleCronstyle();
1
2
3
4
5
6
7
8

6个占位符从左到右分别代表:秒、分、时、日、月、周几

*表示通配符,匹配任意,当秒是*时,表示任意秒数都触发,其它类推

下面可以看看以下传入参数分别代表的意思

每分钟的第59秒触发: '59 * * * * *'
每小时的1分59秒触发 :'59 1 * * * *'
每天的凌晨1点1分59秒触发 :'59 1 1 * * *'
每月的1日1点1分59秒触发 :'59 1 1 1 * *'
2020年的1月1日1点1分59秒触发 :'59 1 1 1 2020 *'
每周天的1点1分30秒触发 :'30 1 1 * * 0'
1
2
3
4
5
6

# 基于日期的计划

const scheduleCronstyle = () => {
  const date = new Date(2020, 4, 26, 15, 10, 10);
  const j = schedule.scheduleJob(date, function(){
    console.log('scheduleObjectLiteralSyntax:' + new Date());
  });
};
1
2
3
4
5
6

# 对象文本语法定时器

second (0-59)
minute (0-59)
hour (0-23)
date (1-31)
month (0-11)
year
dayOfWeek (0-6) Starting with Sunday
1
2
3
4
5
6
7
const scheduleCronstyle = () => {
// 周末的10点11分
  const j = schedule.scheduleJob({hour: 10, minute: 11, dayOfWeek: 0}, function () {
    console.log('hi 小哥明天该上班了啊:' + new Date());
  });
};
1
2
3
4
5
6

# 设置开始时间和结束时间

// 5秒后运行,并在10秒后停止
let startTime = new Date(Date.now() + 5000);
let endTime = new Date(startTime.getTime() + 5000);
var j = schedule.scheduleJob({ start: startTime, end: endTime, rule: '*/1 * * * * *' }, function(){
  console.log('Time for tea!');
});
1
2
3
4
5
6

# 取消定时器

const scheduleCronstyle = () => {
// 周末的10点11分
  const j = schedule.scheduleJob({hour: 10, minute: 11, dayOfWeek: 0}, function () {
    console.log('hi 小哥明天该上班了啊:' + new Date());
  });

  setTimeout(function () {
    console.log('定时器取消');
    // 定时器取消
    j.cancel();
  }, 5000);
};
1
2
3
4
5
6
7
8
9
10
11
12

写在之后

定时器功能大部分需求都可以借助node-schedule完成了,用它在项目中使用效果也不错,各种需求可以满足^_^!

# qrcode-terminal在终端输出二维码

qrcode-terminal (opens new window)在终端输出二维码

安装

yarn add qrcode-terminal -D
1

使用

const qrcode = require('qrcode-terminal');

const url = 'http://www.jing999.cn';
qrcode.generate(url);
// 第二个参数 {small:true} 第三个参数是回调
1
2
3
4
5

# node爬虫

# superagent / axios / node-fetch 请求数据

superagent 拓展性强,同时支持 Node.js 和浏览器,通过构建插件可以实现更多功能 ;缺点:其 API 不符合任何标准
axios 支持 Promise API,同时支持 Node.js 和浏览器
node-fetch 轻量级的模块,与window.fetchAPI 保持一致,在nodeJs中使用

# cheerio

cheerio (opens new window) 可以理解为一个Node.js版本的jquery

let cheerio = require('cheerio');
let $ = cheerio.load("<div id='helloworld'>hello world</div>", {ignoreWhitespace: true...})
1
2

# 选择器

 






 














 










 







// 属性操作
.attr(name, value)
.removeAtrr(name)
.hasClass(className)
.addClass(className)
.remoteClass([className])

// 遍历
.find(selector)
.parent()
.next()
.prev()
.siblings()
.children( selector )
.each( function(index, element) )
.map( function(index, element) )
.filter( selector )
.filter( function(index) )
.first()
.last()
.eq(i)

// 操作DOM
.append( content, [content, ...] )
.prepend( content, [content, ...] )
.after( content, [content, ...] )
.before( content, [content, ...] )
.remove( [selector] )
.replaceWith( content )
.empty()
.html( [htmlString] )
.text( [textString] )

// 其他
$.html()
$('ul').text()
.toArray()
.clone()
$.root()
$.contains( container, contained )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# iconv-lite

有些时候,获取到的数据是一些乱码,尤其是中文的情况.所以我们需要解决乱码的问题,iconv-lite模块就可以解决这一问题.

homeBody = iconv.decode(homeBody,"GBK"); //进行gbk解码
1

如果有乱码就在 cheerio.load()之前进行解码

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> //这里是utf-8,就不需要解码
1

如果是静态页面上面的操作基本上就已经完成了,但是现实中毕竟都是spa 大行其道很多都不是静态的,所以不能用 cheerio 模块来解析。

# 动态页面爬取

# ora 控制台进度条

const ora = require('ora')
const spinner = ora({text: 'loading......', color: 'red'})
spinner.start()
setTimeout(() => {
  spinner.stop()
}, 2000)
1
2
3
4
5
6

# yargs 命令行参数解析工具

const yargs = require('yargs');
console.log(yargs.argv);

// node yargs.js -f -l false  --force
// { _: [
1
2
3
4
5

# inquirer 终端命令行交互工具

const inquirer = require('inquirer')
inquirer.prompt([{
  type: 'checkbox', // list、input、number、confirm、rawlist、expand、checkbox、password、editor
  choices: ['scss', 'ts', 'vuex'],
  name: 'type',// 字段名
  message: '添加项目功能',
  default: true
}]).then((answers) => {
  console.log('结果为:');
  console.log(answers)
})
1
2
3
4
5
6
7
8
9
10
11

添加项目功能 (Press to select, to toggle all, to invert selection, and to proceed) ❯◯ scss ◉ ts ◉ vuex