霞觀小刺 發表於 2019-11-1 15:30:00

node.js(1)

<p><img src="https://img2018.cnblogs.com/blog/1734707/201911/1734707-20191101150556350-803040587.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>node.js基于谷歌的v8引擎,将谷歌的v8引擎单独拿出来 运行在服务器端</strong></p>
<p>&nbsp;<img src="https://img2018.cnblogs.com/blog/1734707/201911/1734707-20191101150958003-153631691.png"></p>
<p>&nbsp;</p>
<h3>&nbsp;全局对象</h3>
<h3>Node.js&nbsp; &nbsp;----&gt;&nbsp; &nbsp; &nbsp;global</h3>
<p>在一个文件中,声明的变量和函数都属于局部变量或局部函数,在交互模式下属于全局变量或全局函数;</p>
<p>例1:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> a=1<span style="color: rgba(0, 0, 0, 1)">;
console.log(global.a);</span></pre>
</div>
<p>例2:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> fn(){
    console.log(</span>123<span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 0, 255, 1)">var</span> res=<span style="color: rgba(0, 0, 0, 1)">global.fn();
console.log(res);</span></pre>
</div>
<h3>&nbsp;JS:window</h3>
<p>在浏览器下,文件中声明的变量或创建的函数都属于是全局作用域下的,可以使用全局对象访问;</p>
<p>xxx.html</p>
<div class="cnblogs_code">
<pre>&lt;script src="xxx.js"&gt;&lt;/script&gt;</pre>
</div>
<p>xxx.js</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> a=1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> fn(){
    console.log(</span>123<span style="color: rgba(0, 0, 0, 1)">);
}
console.log(a);
fn()
console.log(window.a);
window.fn();</span></pre>
</div>
<h3>&nbsp;console对象</h3>
<div class="cnblogs_code">
<pre>global.console.log(123);      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印消息</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 返回值:123</span>
global.console.info(123);   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印消息</span>
global.console.warn(123);   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印警告消息,感叹号</span>
global.console.error(123);    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印错误消息,叉号</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 开始计时</span>
global.console.time('for-loop'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">var</span> i=0;i&lt;10;i++<span style="color: rgba(0, 0, 0, 1)">){

}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 计时结束</span>
global.console.timeEnd('for-loop');</pre>
</div>
<h3>process对象 ---&gt;&nbsp; 查看当前计算机的进程</h3>
<p>语法:process.arch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 多少当前cpu架构</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;process.platform&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 什么当前操作系统</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;process.env&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 查看当前计算机的环境变量</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;process.version&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 查看node.js的版本号</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;process.pid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 进程编号</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;process.kill(pid)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 杀死进程</p>
<h3>buffer对象 ---&gt;&nbsp; 缓冲区(在内存中存储数据的区域,存储网络传出时的资源)</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建buffer,大小是5个字节</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> buf=global.Buffer.alloc(5,'abcde'<span style="color: rgba(0, 0, 0, 1)">);
console.log(buf);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将buffer数据转成普通字符</span>
console.log(buf.toString());</pre>
</div>
<h3>全局函数</h3>
<p>parseInt / parseFloat / encodeURI / decodeURI&nbsp; / isNaN&nbsp; /&nbsp; isFinite&nbsp; /eval</p>
<div class="cnblogs_code">
<pre>console.log(global.parseInt(3.14));   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3 </span></pre>
</div>
<p>1) 一次性定时器</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定时器,参数1是执行的操作,参数2是间隔的时间(ms),当间隔时间到了就执行回调函数</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> timer = setTimeout(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
    console.log(</span>'响铃'<span style="color: rgba(0, 0, 0, 1)">);
},</span>3000<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清除一次性定时器</span>
clearTimeout(timer);</pre>
</div>
<p>2)周期性定时器</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 周期性定时器,每间隔3秒响一次</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> timer = setInterval(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
    console.log(</span>'响铃'<span style="color: rgba(0, 0, 0, 1)">);
},</span>3000<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清除</span>
clearInterval(timer);</pre>
</div>
<p>例1:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用周期性定时器每隔3秒打印hello,打印三次后清楚定时器</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 初始化1个变量用于记录执行次数</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> i=0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> timer = setInterval(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
    i</span>++<span style="color: rgba(0, 0, 0, 1)">;
    console.log(</span>'hello'<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当i为3的时候清除定时器</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span>(i==3<span style="color: rgba(0, 0, 0, 1)">){
      clearInterval(timer);
    }
},</span>3000);</pre>
</div>
<p>3)立即执行的定时器</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> nextTick先执行,setImmediate后执行;</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 立即执行,放到一组事件的最后执行,没有清除方式;</span>
process.nextTick(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
    console.log(</span>'响铃'<span style="color: rgba(0, 0, 0, 1)">);
});


</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在下一个循环的开头执行</span>
setImmediate(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
    console.log(</span>'响铃'<span style="color: rgba(0, 0, 0, 1)">);
});</span></pre>
</div>
<p><strong><span style="font-size: 18px">模块</span></strong></p>
<p><span style="font-size: 13px">1、模块:自定义模块、核心模块、第三方模块</span></p>
<p><span style="font-size: 13px">2、任意一个文件都是一个模块</span></p>
<p><span style="font-size: 13px">3、exports&nbsp; &nbsp;require&nbsp; &nbsp; module.exports&nbsp; &nbsp;__diename&nbsp; &nbsp; &nbsp;__dirname</span></p>
<p><span style="font-size: 13px">4、导出:module.exports.fn=function(){ }</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
创建两个模块main.js(主模块),circle.js(功能模块);在功能模块中创
建两个函数,传递1个参数,分别获取圆的周长(getLength)和面积(getArea),
导出这两个函数;在主模块中引入功能模块,调用两个方法;</span><span style="color: rgba(0, 128, 0, 1)">*/</span>


<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"></span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getLength(){
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 2*PI*<span style="color: rgba(0, 0, 0, 1)">r;
}

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getArea(){
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> PI*r*<span style="color: rgba(0, 0, 0, 1)">r;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 导出</span>
<strong><span style="color: rgba(255, 0, 255, 1)">module.exports</span></strong>.getLength=<span style="color: rgba(0, 0, 0, 1)">getLength;
module.exports.getArea</span>=<span style="color: rgba(0, 0, 0, 1)">getArea;



</span><span style="color: rgba(0, 128, 0, 1)">// </span><span style="color: rgba(0, 128, 0, 1)"></span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 引入功能模块circle.js,自定义模块格为js结尾,.js可以省略</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> circle=<strong><span style="color: rgba(255, 0, 255, 1)">require('./circle.js'</span></strong><span style="color: rgba(0, 0, 0, 1)"><strong><span style="color: rgba(255, 0, 255, 1)">);   </span></strong>
console.log(circle.getLength(</span>2<span style="color: rgba(0, 0, 0, 1)">));
console.log(circle.getArea(</span>2));</pre>
</div>
<p>5、引入文件模块或目录模</p>
<p><img src="https://img2018.cnblogs.com/i-beta/1734707/201911/1734707-20191107154107327-707879391.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;例1:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">创建模块03_1.js,引入当前目录下的03_2目录模块;
在03_2下创建test.js,导出一个函数fn(打印两个数字
相加),在03_1.js中调用</span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">【03_1.js】</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> test=require('/03_2/test.js'<span style="color: rgba(0, 0, 0, 1)">)
console.log(test.add(</span>2,3<span style="color: rgba(0, 0, 0, 1)">));



</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">【test.js】</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> add(a,b){
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> a+<span style="color: rgba(0, 0, 0, 1)">b;
}
module.exports.add</span>=<span style="color: rgba(0, 0, 0, 1)">add;


</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 【package.json】</span>
{"main":"test.js"}</pre>
</div>
<p>例2:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">在05目录下创建模块05_1.js,引入不带路径的目录模块05_2,
05_2目录中含有hello.js文件(打印一句话)</span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 【05目录下05_1.js模块】</span>
require('05_2'<span style="color: rgba(0, 0, 0, 1)">);


</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> </span>
console.log('hello'<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> </span>
{"main":"hello.js"}</pre>
</div>
<p>&nbsp;</p>
<p><strong><span style="font-size: 18px">包和npm</span></strong></p>
<h3>&nbsp;npm:node package manage</h3>
<p>包:就是一个目录模块,里边包含多个文件,其中有一个文件命名为package,json文件,是包说明文件。</p>
<h3>&nbsp;</h3>
<h3>核心模块--&gt; nodejs官方提供的模块,可以直接引用,不需要创建</h3>
<p><strong>1) 查询字符串模块——quearystring</strong></p>
<p>浏览器向服务器发送请求,传递数据的一种方式</p>
<p><span style="background-color: rgba(204, 255, 204, 1)">parse()&nbsp; &nbsp; 将查询字符串解析为对象</span></p>
<p><span style="background-color: rgba(204, 255, 204, 1)">stringify()&nbsp; &nbsp; 将对象转换为查询字符串</span></p>
<p>http://www.codeboy.com/product_details.html?<span style="color: rgba(255, 0, 255, 1)">lid=5&amp;name=dell</span></p>
<p><span style="color: rgba(255, 0, 255, 1)">例1:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"></span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 引入查询字符串模块</span>
const querystring=<span style="color: rgba(255, 0, 255, 1)">require('querystring');</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 对象   用户引入核心模块</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> str='lid=5&amp;pname=dell'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取查询字符串传递的数据</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 将查询字符串解析为对象</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> obj=<span style="color: rgba(0, 0, 0, 1)">querystring.parse(str);
console.log(obj.lid,obj.dell);

</span><span style="color: rgba(0, 0, 255, 1)">var</span> obj2=<span style="color: rgba(0, 0, 0, 1)">{
    name:</span>'tom'<span style="color: rgba(0, 0, 0, 1)">,
    age:</span>18<span style="color: rgba(0, 0, 0, 1)">,
    sex:</span>1<span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将对象转换为查询字符串</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> str2=<span style="color: rgba(0, 0, 0, 1)">querystring.stringfy(obj2);
console.log(str2);</span></pre>
</div>
<p>&nbsp;例2:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 把百度搜索时的查询字符串解析为对象,获取关键词
ie=utf-8&amp;f=8&amp;rsv_bp=1&amp;rsv_idx=1&amp;tn=baidu&amp;wd=电脑&amp; </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
const querystring</span>=require('querystring'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> str='ie=utf-8&amp;f=8&amp;rsv_bp=1&amp;rsv_idx=1&amp;tn=baidu&amp;wd=电脑&amp;'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> obj=<span style="color: rgba(0, 0, 0, 1)">querystring.parse(str);
console.log(obj.wd);</span></pre>
</div>
<p>2)url模块</p>
<p><strong><span style="background-color: rgba(204, 255, 204, 1)">parse()&nbsp; &nbsp; &nbsp;将url解析为对象</span></strong></p>
<p>protocol&nbsp; &nbsp; 协议&nbsp; &nbsp; hostname&nbsp; &nbsp;主机&nbsp; &nbsp; &nbsp;port&nbsp; &nbsp; 端口&nbsp; &nbsp; &nbsp;pathname&nbsp; &nbsp;文件在服务器上路径&nbsp; &nbsp; query&nbsp; &nbsp; 查询字符串</p>
<p>例1:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入url模块</span>
const url = require('url'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> str='http://www.codebody.com:80/list/details.html?lid=5&amp;pname=dell'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将url解析为对象</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> obj =<span style="color: rgba(0, 0, 0, 1)"> url.parse(str)
console.log(obj);</span></pre>
</div>
<p><strong><span style="background-color: rgba(204, 255, 204, 1)">format()&nbsp; &nbsp; &nbsp; 将对象转化为一个url</span></strong></p>
<p>例1:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> obj=<span style="color: rgba(0, 0, 0, 1)">{
    protocol:</span>'http'<span style="color: rgba(0, 0, 0, 1)">,
    hostname:</span>'www.codebody.com'<span style="color: rgba(0, 0, 0, 1)">,
    port:</span>8080<span style="color: rgba(0, 0, 0, 1)">,
    pathname:</span>'/course/100037.html'<span style="color: rgba(0, 0, 0, 1)">,
    <span style="background-color: rgba(204, 255, 204, 1)">query:{lid:</span></span><span style="background-color: rgba(204, 255, 204, 1)">5,pname:'dell'</span><span style="color: rgba(0, 0, 0, 1)"><span style="background-color: rgba(204, 255, 204, 1)">}</span>
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将对象转换为url</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> str =<span style="color: rgba(0, 0, 0, 1)"> url.format(obj);
console.log(str);</span></pre>
</div>
<p>例2:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 浏览器请求的url为https://www.tmooc.cn:3000/web/1810.html?sid=10&amp;name=tom,获取url中传递的sid和name值</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入查询字符串模块</span>
const querystring=require('querystring'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入url模块</span>
const url = require('url'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> str = 'https://www.tmooc.cn:3000/web/1810.html?sid=10&amp;name=tom'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> obj=<span style="color: rgba(0, 0, 0, 1)">parse(str);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取查询字符串</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> qs =<span style="color: rgba(0, 0, 0, 1)"> obj.query;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 解析查询字符串为对象</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> res =<span style="color: rgba(0, 0, 0, 1)"> querystring(qs);
console.log(res.sid,res.name);</span></pre>
</div>
<p><strong>3)文件系统模块——fs</strong></p>
<p><span style="background-color: rgba(204, 255, 204, 1)">fs.stat(path,callback)/fs.statSync(path)&nbsp; &nbsp; &nbsp; 查看文件的状态,通过回调函数来获取结果。</span></p>
<p><span style="background-color: rgba(204, 255, 204, 1)">path&nbsp; &nbsp; &nbsp;要查看的文件路径</span></p>
<p><span style="background-color: rgba(204, 255, 204, 1)">callback&nbsp; &nbsp; &nbsp;回调函数</span></p>
<p><span style="background-color: rgba(204, 255, 204, 1)">&nbsp; &nbsp; err&nbsp; &nbsp; &nbsp; &nbsp;如果查看失败的错误信息</span></p>
<p><span style="background-color: rgba(204, 255, 204, 1)">&nbsp; &nbsp; stats&nbsp; &nbsp; 文件状态信息</span></p>
<p><span style="background-color: rgba(204, 255, 204, 1)">  isDirectory()&nbsp; &nbsp;是否为目录</span></p>
<p><span style="background-color: rgba(204, 255, 204, 1)">&nbsp; &nbsp; &nbsp; &nbsp;isFile()&nbsp; &nbsp; 是否为文件</span></p>
<p>&nbsp;</p>
<p><span style="background-color: rgba(255, 204, 153, 1)">对比同步和异步的区别?</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">  同步会组织后续代码的主席那个,只有方法执行完才能继续执行后边的代码;是通过返回值来获取结果;</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">  异步不会阻止后续代码的执行,把执行的结果放到整个程序的最后执行;通过回调函数来获取结果;</span></p>
<p>&nbsp;</p>
<p>例1:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入fs模块</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看文件状态</span>
fs.stat('C:/Users/ecarx/Desktop/ffwdx200.jtl',(err,stats)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> err   如果有错误信息,会存储再err中</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> stats   文件的具体状态信息</span>
<span style="color: rgba(0, 0, 0, 1)">    console.log(stats);
    console.log(err);
});</span></pre>
</div>
<p>例2: <span style="background-color: rgba(204, 255, 204, 1)">有错误时抛出错误(异步)</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入fs模块</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看文件状态</span>
fs.stat('C:/Users/ecarx/Desktop/ffwdx200.jtl',(err,stats)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> err   如果有错误信息,会存储再err中</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> stats   文件的具体状态信息</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果有错误,抛出错误,阻止程序往后运行</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span>(err) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> err;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看文件是目录形式还是文件形式</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看是否为目录</span>
<span style="color: rgba(0, 0, 0, 1)">    console.log(stats.isDirectory());
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看是否为文件按</span>
<span style="color: rgba(0, 0, 0, 1)">    console.log(stats.ifFile());
});<br>console.log(123);    // 不会组组织后续代码执行</span></pre>
</div>
<p><span style="background-color: rgba(204, 255, 204, 1)">例3:使用同步</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用同步statSync</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> res = fs.statSync('C:/Users/ecarx/Desktop/ffwdx200.jtl'<span style="color: rgba(0, 0, 0, 1)">)
console.log(res.isDirectory());
console.log(res.isFail());<br><br></span>console.log(123);      // 同步方法,会阻止后续代码执行</pre>
</div>
<p><strong>创建目录:</strong></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">fs.mkdir(path,callback)/fs.mkdirSync(path)&nbsp; &nbsp;&nbsp;</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">path&nbsp; 要创建的目录的路径</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">callback&nbsp; 回调函数,只有一个参数</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">&nbsp; &nbsp; &nbsp; err&nbsp; &nbsp; &nbsp;如果创建失败的错误信息</span></p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建目录——异步方法</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入fs模块</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
fs.mkdir(</span>'mydir',(err)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(err) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> err;
    console.log(</span>'目录创建成功'<span style="color: rgba(0, 0, 0, 1)">);
});
console.log(</span>123<span style="color: rgba(0, 0, 0, 1)">);


</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建目录——同步方法</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 引入fs模块</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> res = fs.mkdirSync('mydir'<span style="color: rgba(0, 0, 0, 1)">);
console.log(res);
console.log(</span>123);</pre>
</div>
<p><strong>删除目录:</strong></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">fs.rmdir(path,callback)/fs.rmdirSync(path)</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">path&nbsp; &nbsp; &nbsp;要删除的目录的路径</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">callback&nbsp; &nbsp; &nbsp;回调函数,获取函数的结果</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">  err&nbsp; &nbsp; 如果删除失败的错误信息</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除目录</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 异步</span>
fs.rmdir('mydir',(err)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(err) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> err;
    console.log(</span>'删除成功'<span style="color: rgba(0, 0, 0, 1)">);   </span><span style="color: rgba(0, 0, 0, 1)">
});<br></span></pre>
<pre><span>console.log(123<span>);<br><br></span></span></pre>
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 同步</span> <br>const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">); <br></span><span style="color: rgba(0, 0, 255, 1)">var</span> res = fs.rmdirSync('mydir'<span style="color: rgba(0, 0, 0, 1)">); <br>console.log(res); console.log(</span>123);</pre>
</div>
<p><strong>读取目录</strong></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">fs.readdir(path,callback)/fs.readdirSync(path)</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">path&nbsp; &nbsp; &nbsp;要读取的目录的路径</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">callback&nbsp; &nbsp; &nbsp;回调函数,获取函数的结果</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">  files&nbsp; &nbsp; 读取的文件,返回数组</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">  err&nbsp; &nbsp; 如果读取失败的错误信息</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 读取目录(异步)</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
fs.readdir(</span>'05',(err,files)=&gt;<span style="color: rgba(0, 0, 0, 1)">{

    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(err) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> err;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> files如果没有错误,读取的文件是一个数组</span>
<span style="color: rgba(0, 0, 0, 1)">    console.log(files);
})
console.log(</span>123<span style="color: rgba(0, 0, 0, 1)">);


</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 读取目录(同步)</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> files = fs.readdirSync('05'<span style="color: rgba(0, 0, 0, 1)">);
console.log(files);
console.log(</span>123);</pre>
</div>
<p>例子:组合</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 练习:创建目录hello,读取03_2下的所有文件,删除目录hello</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入fs模块</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
fs.mkdir(</span>'hello',(err)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(err) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> err;
    console.log(</span>'hello目录创建成功'<span style="color: rgba(0, 0, 0, 1)">)
});
fs.readdir(</span>'03_2',(err,files)=<span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(err) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> err;
    console.log(files);
})
fs.rmdir(</span>'hello'<span style="color: rgba(0, 0, 0, 1)">,(err){
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(err) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> err;
    console.log(</span>'删除成功'<span style="color: rgba(0, 0, 0, 1)">);
});</span></pre>
</div>
<p><strong>创建文件</strong></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">fs.writeFile(path,data,callback)&nbsp; &nbsp; 写入文件/创建文件</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">data&nbsp; &nbsp;要写入的数据</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">如果文件不存在则创建文件,然后写入,若已存在,则清空内容写入;</span></p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建文件,每次写入会先清空后写入</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入fs模块(异步)</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
fs.writeFile(</span>'data.txt','hello',(err)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(err) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> err;
});</span></pre>
</div>
<p>&nbsp;</p>
<p>例1:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 练习:创建目录mydir,在该目录下创建文件1.txt,写入1,创建文件2.txt,写入2,
读取mydir下所有文件;删除mydir整个,过程全部使用同步方法。</span><span style="color: rgba(0, 128, 0, 1)">*/</span>


<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入fs模块</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> creat = fs.mkdirSync('mydir'<span style="color: rgba(0, 0, 0, 1)">);
console.log(creat);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> file1 = fs.writeFileSync('1.txt',1<span style="color: rgba(0, 0, 0, 1)">);
console.log(file1);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> file2 = fs.writeFileSync('2.txt',2<span style="color: rgba(0, 0, 0, 1)">);
console.log(file2);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> all = fs.readdirSync('mydir'<span style="color: rgba(0, 0, 0, 1)">);
console.log(all);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> rm = fs.rmdirSync('mydir'<span style="color: rgba(0, 0, 0, 1)">)
console.log(rm);</span></pre>
</div>
<p><strong>读取文件(返回的数据是buffer形式)</strong></p>
<p><span style="background-color: rgba(204, 255, 204, 1)">fs.readFile(path,callback)/fs.readFileSync(path)</span></p>
<p><strong>追加写入(若文件不存在则创建文件,若文件已存在,则在末尾写入数据)</strong></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">fs.appendFile(path,data,callback)</span></p>
<p><span style="background-color: rgba(255, 204, 153, 1)">fs.appendFileSync(path,data)</span></p>
<p>&nbsp;<strong>例2:</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 创建文件user.txt,每次写入一个对象{uid:1,uname:'tom',upwd:'123456'}</span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入fs模块(异步)</span>
const fs = require('fs'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> user = {uid:1,uname:'tom',upwd:'123456'<span style="color: rgba(0, 0, 0, 1)">};
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历对象获取数据</span>
<span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">var</span> key <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> user){
    console.log(key</span>+'---'+<span style="color: rgba(0, 0, 0, 1)">user);
    fs.appendFileSync(</span>'user.txt',user+' '<span style="color: rgba(0, 0, 0, 1)">);

}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 每次写完数据加换行</span>
fs.appendFileSync('user.txt','\n');</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/hd-test/p/11777295.html
頁: [1]
查看完整版本: node.js(1)