`WEB`分类下的文章

技术

命令行参数处理

简介

TODO

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
const sunbo = require('../util/file');
const path = require('path');

// 格式化路径
var root = path.normalize("E:\\opt\\服务器");
var destRoot = path.normalize("E:\\opt\\我的");
//解析需要遍历的文件夹,我这以E盘根目录为例
var filePath = path.resolve(root);
//是否覆盖原文件 (true覆盖)
var flag = false;
var str = "share: true";
//const str = " ";


function handleArgs() {

if (process.argv.length > 0) {
//console.log(process.argv);
process.argv.forEach(function(arg, index) {
let param = "";

switch (arg.trim()) {
case '-f':
param = process.argv.slice(index + 1)[0];
// console.log(param);
filePath = path.resolve(path.normalize(param));
console.log("源文件/夹 " + filePath);
break;
case '-s':
param = process.argv.slice(index + 1)[0];
str = param;
console.log("字符串 " + str);
break;
case '-o':
param = process.argv.slice(index + 1)[0];
console.log(param);
if (param == "true") {
flag = true;
}
console.log("覆盖 " + flag);
break;

case '-r':
param = process.argv.slice(index + 1)[0];
root = param;
console.log("源文件/夹的根目录 " + root);
break;
case '-d':
param = process.argv.slice(index + 1)[0];
destRoot = param;
console.log("源文件/夹的根目录" + destRoot);
break;
case '-h':
console.log('-f ');
console.log('-s ');
console.log('-o ');
console.log('-r ');
console.log('-d ');
console.log('----------------------');
break;
default:

break;
}
});
}

}

handleArgs();

测试

1
node www.js -o true -f E:\\opt\\服务器

输出

1
2
覆盖 true
源文件/夹 E:\opt\服务器

说明

参考文档:slice/splice

1
2
3
const args = process.argv.slice(2);
//const args = process.argv.splice(2);
console.log("args=" + args);

//运行 node test.js zhou jason china

//结果如下

1
args=zhou,jason,china

第三方模块 commander

https://blog.csdn.net/weixin_40817115/article/details/81699737

参考
https://blog.csdn.net/tyler_download/article/details/51066285

https://blog.csdn.net/corner2030/article/details/78528153

阅读剩下更多

默认配图
技术

文件读取

###

普通复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 复制文件 (异步,覆盖原文件)
* @param {*} source 源文件
* @param {*} dest 目标文件
*/
function mycopy(path1, path2) {
fs.readFile(path1, function(err, data) {
if (err) {
console.log(err);
} else {
var data1 = data.toString();
fs.writeFile(path2, data1, function(err) {
if (err) {
console.log(err);
} else {
console.log("复制成功");
}
})
}
})
}

pipe复制文件

例子:

1
2
3
4
5
6
7
8
9

// 1.引入模块
let fs =require('fs');
var request = require('request');
//2.创建读入流
let rs = fs.createReadStream('D:/Pictures/Saved Pictures/testSP.mp4'); //要读取视频的位置
let ws = fs.createWriteStream('testSP.mp4'); //视屏要写入的位置
//创建管道
rs.pipe(ws);

详细

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

// 1.引入模块
let fs =require('fs');
//2.创建读入流
let rs = fs.createReadStream('D:/Pictures/Saved Pictures/testSP.mp4'); //要读取视频的位置
let ws = fs.createWriteStream('testSP.mp4'); //视屏要写入的位置,我这里是默认的项目文件夹下的

//3.监听流的打开和关闭
ws.once('open' ,()=>{
console.log("读入通道打开");
});
ws.once('close' ,()=>{
console.log("读入通道以关闭");
});

rs.once('open' ,()=>{
console.log("写出通道已打开");
});
rs.once('close' ,()=>{
console.log("写出通道已关闭");
});
//4.绑定data
rs.on("data", (data)=>{
ws.write(data);
});

参考:

https://blog.csdn.net/qq_41542894/article/details/82291555

https://www.jianshu.com/p/21c7db836836

文件操作
https://www.jianshu.com/p/482a2d272a21?utm_source=oschina-app

文件的同步和异步写入操作
https://blog.csdn.net/qq_41542894/article/details/82229607

官网

http://nodejs.cn/api/stream.html#stream_event_pipe

阅读剩下更多

默认配图
技术

node递归文件夹

###
TODO

方案1

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
var fs = require('fs');
var path = require('path');

//解析需要遍历的文件夹,我这以E盘根目录为例
var filePath = path.resolve('E:');

//调用文件遍历方法
fileDisplay(filePath);

/**
* 文件遍历方法
* @param filePath 需要遍历的文件路径
*/
function fileDisplay(filePath){
//根据文件路径读取文件,返回文件列表
fs.readdir(filePath,function(err,files){
if(err){
console.warn(err)
}else{
//遍历读取到的文件列表
files.forEach(function(filename){
//获取当前文件的绝对路径
var filedir = path.join(filePath,filename);
//根据文件路径获取文件信息,返回一个fs.Stats对象
fs.stat(filedir,function(eror,stats){
if(eror){
console.warn('获取文件stats失败');
}else{
var isFile = stats.isFile();//是文件
var isDir = stats.isDirectory();//是文件夹
if(isFile){
console.log(filedir);
}
if(isDir){
fileDisplay(filedir);//递归,如果是文件夹,就继续遍历该文件夹下面的文件
}
}
})
});
}
});
}
1
2
3
4
5
6
如果碰到有中文不能解析的html,这样写
var cheerio = require('cheerio');
var iconv = require('iconv-lite');
var myHtml = fs.readFileSync("index.html");
var myHtml2 = iconv.decode(myHtml, 'gbk');
console.log(myHtml2);

说明:异步的方式读取

方案2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;

function readFileList(dir, filesList = []) {
//同步读取
const files = fs.readdirSync(dir);
console.log(files);
files.forEach((item, index) => {
var fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
readFileList(path.join(dir, item), filesList); //递归读取文件
} else {
filesList.push(fullPath);
}
});
return filesList;
}

var filesList = [];
readFileList(__dirname,filesList);
console.log(filesList);

说明:同步的方式读取

参考

https://blog.csdn.net/younglao/article/details/77046830

https://blog.csdn.net/liyazhen2011/article/details/87882180

阅读剩下更多

默认配图
技术

入门

简介

官方下载地址:
https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.7.15

window安装
https://yq.aliyun.com/articles/647403

https://gitee.com/Jicklin/rabbitMq/blob/master/rabbitMQ_producer/src/main/resources/application.properties
https://blog.csdn.net/u011059021/article/details/79827793

https://blog.csdn.net/zhangyuxuan2/article/details/82986702
https://blog.csdn.net/u013871100/article/details/82717110

https://www.cnblogs.com/vipstone/p/9275256.html

阅读剩下更多

默认配图
返回顶部