日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

NodeJS必知基础知识(非巨详细)

發(fā)布時間:2023/12/16 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NodeJS必知基础知识(非巨详细) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Node.js是啥?

node.js是構建在Chrome v8 引擎上的一個javascript 運行環(huán)境

node和Chrome一樣都是基于事件驅動的異步架構!Chrome是基于事件的交互,而node是基于事件的I/O;

node沒有HTML、Webkit和顯卡驅動等UI技術支持;

文件引入

如果當前文件夾下有index.js和2.js
2.js 里包含變量a(let a=1)

//2.js let a=1 global.b=ouyang; //掛載在global全局下,node的全局是global不是window //index.js const obj=require("./2.js"); //require請求的就是module.exports的內容 console.log(obj.a); //輸出2.js里的a不會成功 console.log(global.b); //ouyang 這樣才會成功

node在執(zhí)行時會把代碼重新編譯,編譯時會把代碼打包成字符串放入一個函數(shù)里進行編譯,所以直接在全局var 或者let聲明的變量并不能通過全局直接調用!

模塊

//02.js module.exports=function(){console.log("666") }; //被覆蓋 module.exports=123123123; //為模塊賦值 // module.exports.num=123; //可以為模塊定義屬性 //index.js const obj=require("./2.js"); //如果不加./ 則在核心模塊或第三方依賴模塊里找,就不再當前文件夾下找了 console.log(obj); //123123123

重新定向

//2.js exports=module.exports exprots.fn=function(){}; //對exports重新定向,并添加新的屬性fn,方便index.js的require訪問;

node把所有抽象成了事件,node執(zhí)行時,同步異步差異如下:

// 同步 process.nextTick(()=>{ console.log("1") //2 }) process.nextTick(()=>{console.log("2") //3 }) //異步 setImmediate(()=>{console.log("3") //6process.nextTick(()=>{console.log("4")//8} }) setImmediate(()=>{console.log("5") //7 }) setTimeout(function(){console.log("8") //5 },0) //同步 process.nextTick(()=>{console.log("7") //4 }) console.log("6") //1

node 輸出結果 6 1 2 7 8 3 5 4

事件隊列,自動排隊
macro-task:script(全部代碼) (setInterval setTimeout定時器,同優(yōu)先級誰先注冊誰高) setImmediate I/O

micro-task:process.nextTick Promise=>then

同步的是整體代碼,而異步之所以異步是將參數(shù)里的回調函數(shù)在將來執(zhí)行

node執(zhí)行初期執(zhí)行一個無線循環(huán)
while(true){
1.scipt(全部代碼),產生同步異步;
2.執(zhí)行完script后 將micro-task 隊列里的事件全部執(zhí)行;
3.再次執(zhí)行macro-task 隊列里的第二層script,產生第二層的micro-task的
4.緊接著執(zhí)行micro-task里新產生的事件隊列
一直到最里層的macro-task代碼產生的micro-task隊列執(zhí)行完畢…
}

Promise.resolve("1").then(res=>console.log(res)) //Promise是同步的,then是異步的(將來執(zhí)行的) process.nextTick((=>{console.log("2") }) setTimeout((=>{console.log("3") },0); console.log("4")

第一次循環(huán):
macro-task:script(第一層代碼,檢查到有l(wèi)og 4 ) setTimeout=>3
micro-task:process.nextTick=>2 Promise=>then 1
第二次循環(huán):
macro-task:{script(第一層代碼,檢查到有l(wèi)og 4 )}->第一次已執(zhí)行 setTimeout=>3->第二次正在執(zhí)行
micro-task:{process.nextTick=>2 Promise=>then 1}->第一次已執(zhí)行
mode輸出結果 4 2 1 3

總結優(yōu)先級,可以簡單看為:

nextTick > Promise.then > setTimeout > setImmediate

下載包

npm.nodejs.com–>sort by keywords search

運行使用nodejs

實現(xiàn)在控制臺進入當前文件夾,node + 文件名 回車執(zhí)行

  • 初始化項目信息
//初始化項目環(huán)境 npm init //確認初始化對象,輸入名稱 package name: nodetest //確認版本等信息,默認回車//直到出現(xiàn)Is this OK? 回車 //初始化后生成一個package.json 文件,就是上面的配置信息。npm init -y //一路回車默認配置信息
  • 安裝模塊
npm install 模塊名 參數(shù) //npm install webpack -g 全局安裝webpack包,同時安裝多個包用空格隔開//安裝時會把相關依賴的包同時安裝在當前控制臺所在的目錄路徑 //如安裝koa包,--save是安裝在當前文件夾,-g是本地,安裝在dependencies里代表上線時會使用的包,而koa-router --save-dev 代表生產環(huán)境使用的包,將在配置文件package.json 的測試用包,-D代表 --save-dev npm install koa --save //引用時: const Koa = require("Koa"); const app = new Koa(); app.use(async(ctx)==>{ctx.body="這是后臺返回的數(shù)據(jù)" })app.listen(3000);//指定版本 npm install koa@7.0.1 //不指定默認最高版本
  • 添加上傳使用用戶
npm adduser Username: 你的名字 Password: (不可見) ...

或者去官網(wǎng)npm.nodejs.com 注冊

//登錄npm login Username: Password: Email://上傳 npm publish //上傳當前初始化的node環(huán)境里的文件,同時會對比以前的文件
  • 如果國外服務器卡頓,使用淘寶鏡像

node原生模塊

  • 事件模塊 events
const events=require("events"); //使用下面的對象時直接加“.EventEmitter”使用 events獲得的是構造函數(shù),用new創(chuàng)建實例

如:

const eventEmitter=require("events").EventEmitter; const myEmitter=new EventEmitter();//這里有一個異步 setTimeout(()=>{//異步的結果出來 },2000);myEmitter.on("someEvents",()=>{console.log("這個某個異步的回調執(zhí)行函數(shù)") }

或者:

const eventEmitter=require("events").EventEmitter; const myEmitter=new EventEmitter(); const fn=()=>{console.log("這個某個異步的回調執(zhí)行函數(shù)") } //這里有一個異步 setTimeout(()=>{//異步的結果出來myEmitter.emit("fengyu") },2000); myEmitter.on("fengyu",fn}; //第一個參數(shù)是綁定的監(jiān)聽名號,第二個是回調函數(shù),異步結果執(zhí)行的函數(shù)
  • 自定義模塊的原型
const eventEmitter=require("events").EventEmitter; const myEmitter=new EventEmitter(); function Fn=(name)=>{this.name = name; }Fn.prototype.__proto__ = EventEmitter.prototype; //將模塊的原型賦值到Fn的默認原型const obj=new Fn("大壞蛋");obj.on("fy",function(){console.log(this.name); }) setTimeout(()=>{//異步的結果出來myEmitter.emit("fy") },2000);//node+文件名 在控制臺執(zhí)行 const eventEmitter=require("events").EventEmitter; const myEmitter=new EventEmitter();myEmitter.on("newlistenner",function(){console.log("綁定了一個新的方法"); })myEmitter.on("fy1",()=>{}); //觸發(fā)log1次 myEmitter.on("fy2",()=>{}); //觸發(fā)log第二次//myEmitter.off可以去除 //myEmitter.getMaxListenners()可以設置myEmitter實例的on函數(shù)可以綁定的最大事件綁定次數(shù),可以用setMaxListenner(數(shù)值)更改,默認10//myEmitter.listenners()可以查看on對第一個參數(shù)名稱相同的事件綁定了多少響應回調函數(shù)
  • path模塊
const path=require('path') console.log(path.join("a","b"))//用于拼接路徑 a/bconsole.log(__dirname); //返回文件夾路徑 console.log(__filename); //返回文件路徑,包含文件名 console.log(path.resolve(__dirname,"test"); //返回一個絕對路徑console.log(path.parse(__filename)); //序列化傳入的參數(shù)如下格式/* {root:'/',dir:'',base:'',name:'' } */

URL模塊

  • 導入URL模塊
const {URL} = require("url"); //建議加上.URL,不適用node自己實現(xiàn)的方法 const myUrl=new URL("https://www.baidu.com"); console.log(myUrl); //解析出整個url的各種值并轉化為json數(shù)據(jù)//search:'' 為查詢部分 //URLsearchParams:'' 這是一個類似的map對象 //如果用parse序列化 const myUrl2=url.parse("https://www.baidu.com");

查詢字符串querystring

const qs =require('querystring');const queryObj=qs.parse(myUrl.search.slice(1)) console.log(queryObj); // 通過. 使用里面的數(shù)據(jù)

斷言 assert

用于判斷得到的值是否為期望值

const assert = require('assert'); //assert(布爾值,報錯信息) assert(true,"如果第一個參數(shù)的布爾值不為true,這個字符串就為報錯信息!") //或:assert(表達式,預期,報錯信息)

加密模塊

const crypto = require('crypto');const KEY="fengyu"; const obj=crypto.createHash('md5');obj.update(KEY);const password=obj.digest('hex'); //輸出剛才的結果,1次 參數(shù)可以選擇進制位數(shù)等

文件操作 js

const js = require('fs')fs.readFile('路徑',(err,data)=>{if(err) threw err;console.log(data); }); //有Sync 同步 沒有回調, 沒有Sync 異步有回調 ,錯誤對象err永遠是第一個參數(shù)位; //如果讀取錯誤,err是個json對象,不為null,包含路徑,結果,權限等鍵與值//輸出的data一般是個buffer數(shù)據(jù),如果添加第二個參數(shù)(進制編碼),可以將data轉換為其他文字編碼,如UTF-8 //異步讀取 const js = require('fs')const fn=async()=>{<!--await fs.readFile("./1.txt","utf8",fucntion(err,data){if(err)return;return data;} //取不到--> const data =await new Promise((resolve,reject)=>{fs.readFile("./1.txt","utf8",(err,data)=>{if(err) return reject(err)resolve(data)}}console.log(data); //取不到 }

//同步讀取 const js = require('fs');const data=fs.readFileSync("./2.txt","utf8");//讀取錯誤立馬報錯 //寫 const js = require('fs'); const data="fengyu"; fs.writeFile("./2.txt",data,'utf8',err=>{if(err) throw err;console.log("寫入成功"); }) //utf8 編碼參數(shù)可選 //寫 const js = require('fs'); const data="fengyu";

創(chuàng)建服務器實例

const http=require('http') const fs=require('fs') const server=http.createServer((req,res)=>{res.writeHead(200,{"Content-Type:"text/plain;charset=utf-8""}); //響應頭,text/plain 是純文本,charset設置文字編碼res.write("向客戶端返回數(shù)據(jù)"); //數(shù)據(jù)響應,返回值res.end()l//結束響應 }); //req requeset, res response //修改后打斷^C,重新執(zhí)行, 或者安裝 npm全局安裝nodemon server.listen(3000); //建議監(jiān)聽1024以上//返回頁面if(req.method==="GET"){res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"})switch(req.url){case "/":res.write(fs.redFileSync("index.html","utf8"));<!--fs.readFile("index.html","utf8",(err,data)=>{res.write(data)res.end();//由于是異步的,fs的readFirefile執(zhí)行在后面的end后,需要把end提到這里面主動提前執(zhí)行end}--><!--如果使用管道流fs.createReadStream("index.html").pipe(res);-->}} const http=require('http') const server=http.createServer((req,res)=>{const obj={a:1,b:2}res.write(JSON.stringfy(obj) }) server.listen(3000,()=>{console.log("服務器監(jiān)聽在localhost:3000") })

框架 Koa

cosnt Koa= require('koa')const app =new Koa//中間件 理論上可以注冊任意多個 洋蔥模型 app.use(async(ctx,next)=>{await next();//提交到下一層 });//ctx context 上下文環(huán)境
  • 路由管理
router.get("/",async (ctx,next)=>{//只有同時滿足在根路徑下get請求方式才能進入此中間件,"/"代表根路徑,可以改成/home /home/index等等console.log(1*)await next()console.log(1=)},async(ctx,next)=>{ //第二個中間件console.log(2*)await next()console.log(2=) }) // 1* 2* 2= 1=app.use(router.routes()),use(router.alloweMethods());//掛載中間件在app上執(zhí)行

RESTfull規(guī)則

npm安裝koa-static 管理靜態(tài)資源

Pug模板引擎

需要 koa pug koa-views(視圖管理)

const Koa=require('koa') const views=require('koa-views')const {join}=require('path') // 使用join鏈接 //pug不需要實例,可以自動識別const app=new Koaapp.use(views(join(__dirname,"views"),{extension:'pug' //模板pug,views是文件夾名,路徑下有個views文件夾 })) //一定要在其他中間件之前注冊模板信息app.use(async(ctx)=>{ctx.render("index.pug") //渲染模板 })app.listen(3000)

MongoDB

  • 安裝

一路默認即可,路徑最好在根目錄,確認安裝環(huán)境,如果沒有要添加到系統(tǒng)的環(huán)境變量里

  • 啟動服務:mongod --dbpath Desktop/demo/db 啟動服務并初始化目錄,完成時會提示連接的監(jiān)聽端口,默認是27017(關閉窗口會斷開連接)
  • 客戶端連接:mongo
  • 查看數(shù)據(jù)庫集合:show dbs
  • 創(chuàng)建數(shù)據(jù)庫:use DATABASE_NAME
  • 查看正在使用的集合:db

概念: 庫database,集合collection

//use 就創(chuàng)建了數(shù)據(jù)庫,并進入當前數(shù)據(jù)庫 > use test //dropDatabase 刪除當前數(shù)據(jù)庫 switched to db test //createCollection創(chuàng)建集合 > db.createCollection("runoob") { "ok" : 1 } > show collections runoob //添加數(shù)據(jù) > db.runoob.insert({"name" : "fengyu","length":"10"}) //db.runoob.drop()刪除集合 //find不加參數(shù)查找當前目標集合的所有數(shù)據(jù),可以傳參,內容為數(shù)據(jù)里的信息 > db.runoob.find()//find({"name":"fengyu"})
  • 其他主機連接服務器數(shù)據(jù)庫
const mongoose=require('mongoose') const db=mongoose.createConnection("mongodb://localhost:27017/fengyu",{userNewRulParser:true})//用es6原生的Promise取代MongoDB自實現(xiàn)的Promise mongoose.Promise=global.Promise//操作數(shù)據(jù)庫之前,使用Schema設置每個字段的數(shù)據(jù)類型db.on("error",console.log.bind(console."數(shù)據(jù)庫連接失敗")) db.on("open",(=>{console.log("數(shù)據(jù)庫連接成功") })//連接成功-->操作數(shù)據(jù)庫-->設置Schemaconst Schema=mongoose.Schema const JavaScriptSchema=new Schema({name:String,age:Number,sex:{type:String,default:"男"} }) const JavaScript=db.model("javascript"/*集合名,默認加s*/,JavaScriptSchema/*定義的數(shù)據(jù)結構*/) //模式化數(shù)據(jù),返回一個對象 const data={name:"大帥比",age:27 }const data2={name:"小清新",age:18,sex:"女" } //插入上面準備的數(shù)據(jù)const d1=new JavaScript(data) //實例,結構化數(shù)據(jù) d1.save().then(err,,res)=>{ //保存,即數(shù)據(jù)庫的創(chuàng)建插入等操作console.log(err) })

總結

以上是生活随笔為你收集整理的NodeJS必知基础知识(非巨详细)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。