君陌离的博客

vuePress-theme-reco 君陌离    2018 - 2020
君陌离的博客 君陌离的博客

Choose mode

  • dark
  • auto
  • light
首页
我的作品
  • 项目橱窗
  • blog模板
分类
  • 数据库
  • CSS
  • 摘记
  • JS
  • Node
  • Vue
  • React
  • GIT
  • Promise
  • Liunx
  • Xshell
  • ajax
  • WINDOWS
  • Python
  • 随笔
  • 脚手架
  • node
  • 自动化
标签
笔记
时间线
About Me
  • 关于我
  • 赞赏
Contact
  • GitHub
  • QQ
author-avatar

君陌离

70

文章

90

标签

首页
我的作品
  • 项目橱窗
  • blog模板
分类
  • 数据库
  • CSS
  • 摘记
  • JS
  • Node
  • Vue
  • React
  • GIT
  • Promise
  • Liunx
  • Xshell
  • ajax
  • WINDOWS
  • Python
  • 随笔
  • 脚手架
  • node
  • 自动化
标签
笔记
时间线
About Me
  • 关于我
  • 赞赏
Contact
  • GitHub
  • QQ

Node指令

vuePress-theme-reco 君陌离    2018 - 2020

Node指令

君陌离 2020-08-07 Node指令

Node

npm淘宝镜像
安装http-server
nodeJs 爬虫
......

# npm淘宝镜像

npm install -g cnpm --registry=https://registry.npm.taobao.org
1

# npm发布

#初始化项目,使用
vue init webpack-simple my-project
npm publish --access=public(公共)
npm publish(私有)
1
2
3
4

# npm login

#输入
npm login
#继续
Username:mrsong
password:####
Email:289097246@qq.com
1
2
3
4
5
6

# npm logout

#命令退出登录
npm logout
1
2

# 查看npm登录状态

npm who am i
1

# npm发布包

#私有包
npm publish
#公开包
npm publish --access=public
1
2
3
4

# 安装http-server

node启动本地服务(安装http-server)

# 安装
npm i http-server -g
# 启动服务
http-server
# 启动服务并制定端口
http-server -a 0.0.0.0 -p 8008
1
2
3
4
5
6

# nodeJs 爬虫

cheerio

const https = require('https')
// const http = require('http') /* 方式二时使用*/
const fs = require('fs')
const cheerio = require('cheerio')
const request = require('request')
const path = require('path');

const imgDir = path.join(__dirname, 'img');

let url = 'https://www.xxxxxx.com/'

https.get(url, (res)=>{
    // 安全判断
    const {statusCode} = res
    const contentType = res.headers['content-type']
    console.log(statusCode, contentType)

    let err = null
    if (statusCode !== 200) {
        err = new Error('请求状态错误')
    } else if (!/^text\/html/.test(contentType)) {
        err = new Error('请求类型错误')
    }

    if (err) {
        console.log(err)
        res.resume() //重置缓存
        return false
    }

    let resData = ''
    res.on('data', (data)=>{
        resData += data.toString('utf8')
    })
    res.on('end', ()=>{
        //将请求数据保存在本地
        let $ = cheerio.load(resData)
        $('img').each((index, el) => {
            let imgUrl = $(el).attr('src')
            // console.log($(el).attr('src'))
            if (imgUrl) {
                let filename = imgUrl.split('/').pop()
                /* 方式一*/
                request('http:'+imgUrl).pipe(fs.createWriteStream(imgDir + '/' + filename));
                /* 方式二*/
                // var req = http.get('http:'+imgUrl, function (res) {
                //     var imgData = "";
                //     res.setEncoding("binary"); //一定要设置response的编码为binary否则会下载下来的图片打不开
                //     res.on("data", function (chunk) {
                //       imgData += chunk;
                //     });
                //     res.on("end", function () {
                //      let filename = imgUrl.split('/').pop()
                //       fs.writeFile(imgDir + '/' + filename, imgData, "binary", function (err) {
                //         if (err) {
                //           console.log("保存失败");
                //         }
                //         console.log("保存成功");
                //       });
                //     });
                //     res.on("error", function (err) {
                //       console.log("请求失败");
                //     });
                //   });
            }
        });
        console.log('数据传输完毕')
    })
}).on('error', (err)=>{
    console.log('请求错误')
})
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
欢迎来到 您的站点名称($site.title)
看板娘