1、Node.js的初步
(1)下載node.js:https://nodejs.org/zh-cn/
(2)配置環(huán)境變量,CMD中根據(jù)node -v 和 npm -v 看是否顯示相應(yīng)的版本號(hào)
(3)推薦使用VS code,在終端可以好的使用node指令來指示服務(wù)端的要求
2、Node.js中的各種模塊的使用和下載
(1)引入模塊使用通過語句:
? ? ? ? ? ? var/const? ?http = require('http');//此時(shí)就引入了http的模塊
(2)node.js可以通過http自身形成服務(wù)器,接下來創(chuàng)建web服務(wù)
? ? ? ? ? ? http.createServer((req,res)=>{
? ? ? ? ? ? ? ? ? ??res.writeHead(200,{"Content-type":"text/html;charset='utf-8'"});//設(shè)置響應(yīng)頭
? ? ? ? ? ? ? ? ? ? res.write();//填入數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? res.end();//結(jié)束響應(yīng)
}).listen(3000);//3000端口號(hào)
? (3)node.js中很多模塊使用都是可以通過npm/cnpm來下載
? ??????https://www.npmjs.com/;這里可以查詢我們想要的模塊
? ? ? ? 通過 npm install? 模塊? --save 就可以引入模塊來使用
(4)初始使用一個(gè)包時(shí),先通過npm init --yes 指令來引入package.json內(nèi)容如下
? ????????{
??????????????"name":?"demo15",//位置
??????????????"version":?"1.0.0",
??????????????"description":?"",
??????????????"main":?"app.js",
??????????????"dependencies":?{//這下面可以顯示我們引入的模塊版本,當(dāng)我們把數(shù)據(jù)上交給別人的時(shí)候是不傳包的
這時(shí)候需要對(duì)方通過 npm i ,把對(duì)應(yīng)模塊的包給引入才可以使用模塊
????????????????????"ejs":?"^3.1.3"
??????????????},
??????????????????"devDependencies":?{},
??????????????????"scripts":?{
????????????????????????????"test":?"echo?\"Error:?no?test?specified\"?&&?exit?1"
??????????????????????????????},
??????????????????"keywords":?[],
??????????????????"author":?"",
??????????????????????"license":?"ISC"
????????????????????????}
(5)常用的模塊:
? ? ? ? ? ? const fs = require("fs");
? ? ? ? ? ? const url = require("url");
? ? ? ? ? ? const path = require("path");
? ? ? ? ? ? const http = require("http");
? ? ? ? ? ? const ejs? =? require("ejs");
????????????var?md5?=?require('md5');//? ? md5('123456');進(jìn)行加密對(duì)數(shù)字加密
3、node.js的兩種暴露使用方法和使用基礎(chǔ)
(1)node.js是在.js的環(huán)境下編寫的,當(dāng)想獲取別的js資源的時(shí)候,和引入模塊一樣
? ? const? 自定義名 = require('js路徑');
(2)exports 和 module.exports;
? ? ? ? 當(dāng)方法是一個(gè)對(duì)象的使用使用module.exports = object;
? ? ? ? 當(dāng)方法是獨(dú)立的一個(gè)個(gè)function的時(shí)候使用exports.函數(shù)名 = 函數(shù);
? ? 這樣把方法暴露出來,才可以方便調(diào)用。
4、fs模塊的使用功能
(1)fs.stat? 檢查是文件還是目錄;fs.stat(路徑,(err,data)=>{兩個(gè)參數(shù),err錯(cuò)誤顯示,data返回結(jié)果}
? ? ? ? ? ? data.isFile()//是文件則true,data.isDirectory();//目錄則true;
(2)fs.mkdir? 創(chuàng)建目錄,可以添三個(gè)參數(shù)
? ? ? ? path? 將創(chuàng)建目錄的路徑
? ? ? ? ?mode? 目錄讀寫權(quán)限,默認(rèn)777
? ? ? ? callback? 回調(diào)函數(shù),傳遞異常函數(shù)err
(3)fs.writeFile? 創(chuàng)建寫入文件
? ? ? ? 第一個(gè)參數(shù):path? 寫入路徑
? ? ? ? ?第二個(gè)參數(shù):寫入的內(nèi)容
? ? ? ? 第三個(gè)回調(diào)函數(shù),callback一個(gè)異常參數(shù)err
(4)fs.appendFile? 追加文件
? ? ? ? 沒有則創(chuàng)建,存在則后面追加內(nèi)容
? ? ? ? fs.appendFile('./css/base.css','body{color:red}',(err)=>{});//創(chuàng)建一個(gè)css文件,內(nèi)容樣式是body
? ? ? ??fs.appendFile('./css/base.css','h2{color:red}',(err)=>{});//文件已存在,則內(nèi)容追加,且在body的后面
(5)fs.readFile? 讀取文件? ?(path,? (err,data)=>{})
? ? data可以返回文件內(nèi)容,一般是十六進(jìn)制返回,需要data.toString()轉(zhuǎn)換成字符串
(6)fs.rename 1、重命名,2、移動(dòng)文件
? ? ? ? fs.rename('./css/base.css','./css/index.css',(err)=>{});//此時(shí)base.css重命名為index.css
? ? ? ? fs.rename('./css/base.css','./html/base.css',(err)=>{})//此時(shí)base.css,移動(dòng)了從css包移到了html包中
(7)fs.rmdir? ?刪除目錄? (path,(err)=>{});
(8)fs.unlink? 刪除文件? (path,(err)=>{});