Node | 基礎(chǔ)

nodejs是什么

node.js 是一個(gè)基于 Chrome V8 引擎的 JavaScirpt 運(yùn)行環(huán)境。

nodejs安裝

  • 官網(wǎng)下載對(duì)應(yīng)系統(tǒng)的穩(wěn)定版本即可 https://nodejs.org/en/
  • 打開(kāi)終端,輸入下面兩條命令,可以顯示對(duì)應(yīng)的版本號(hào),表示安裝成功
    • node -v
    • npm -v
      image.png

使用node運(yùn)行js文件

  • node xx/xx/xx.js

npm

  • npm是node package manager的縮寫,即nodejs軟件包管理者
  • npm 在安裝node時(shí)一起被安裝
  • npm使用國(guó)外鏡像,由于網(wǎng)絡(luò)原因,可能會(huì)導(dǎo)致安裝包失敗。可以切換為淘寶鏡像
npm i -g cnpm --registry=https://registry.npm.taobao.org
// 測(cè)試是否安裝成功
cnpm -v 

--save和 --save-dev的區(qū)別

  • --save安裝的插件在package.json文件的dependencies對(duì)象中
  • --save-dev安裝的插件在package.json文件的devDependencies對(duì)象中
  • devDependencies是開(kāi)發(fā)環(huán)境使用,dependencies是生產(chǎn)環(huán)境使用。

common.js模塊化

  • 創(chuàng)建一個(gè)模塊, 簡(jiǎn)單理解就是: 一個(gè)js文件就是一個(gè)模塊
  • 導(dǎo)出一個(gè)模塊modules.exports = name
  • 引用一個(gè)模塊并且調(diào)用reqiure("xx/xxx.js")
    導(dǎo)出
    image.png

引用


image.png

安裝nodemon插件,檢測(cè)文件變化,自動(dòng)重啟node。 示例:

  • 初始化環(huán)境 npm init
  • 安裝插件 npm i nodemon
  • package,json文件配置項(xiàng)
  • image.png
  • 啟動(dòng)項(xiàng)目 npm run start

http模塊是nodejs自帶模塊

該模塊提供了requestresponse對(duì)象,用來(lái)處理數(shù)據(jù)請(qǐng)求和響應(yīng)。

  • request對(duì)象封裝了http請(qǐng)求
  • response對(duì)象封裝了http響應(yīng)

啟動(dòng)本地服務(wù)

  1. 引入http模塊
  2. 創(chuàng)建http服務(wù)器
  3. 監(jiān)聽(tīng)端口號(hào)
  4. 啟動(dòng)本地服務(wù)器 node xx.js
  5. 瀏覽器訪問(wèn) localhost:port
const http = require('http')
const server =  http.createServer((reqt, res) => {
  
    res.end("This is a server by Node.js");
});
server.listen('3000',() => {
    console.log('3000 port is linsening')
})

request對(duì)象下的方法和屬性

url 獲取請(qǐng)求地址
method 獲取請(qǐng)求方法(GET/POST)

response對(duì)象下的方法

end 返回信息給前端
writeHead 設(shè)置返回信息的格式。

  • 識(shí)別HTML結(jié)構(gòu)
    res.writeHead(200,{'Content-Type':'text/html'});
  • 識(shí)別json數(shù)據(jù)
    res.writeHead(200,{'Content-Type':'application/json'});
  • 識(shí)別文本結(jié)構(gòu)
    res.writeHead(200,{'Content-Type':'text/plain'});

querystring模塊

querystring是nodejs自帶的模塊,直接引入使用即可,從字面上的意思就是查詢字符串,一般是對(duì)http請(qǐng)求所帶的數(shù)據(jù)進(jìn)行解析

  • querystring.parse(str,separator,eq,options) parse這個(gè)方法是將一個(gè)字符串反序列化為一個(gè)對(duì)象。

使用postman測(cè)試post請(qǐng)求 處理post data傳遞的數(shù)據(jù)

image.png

獲取post請(qǐng)求發(fā)送的數(shù)據(jù)

if (path === '/api/create' && method === 'POST') {
const reqType = req.headers['content-type'] //獲取到請(qǐng)求的數(shù)據(jù)格式

let bodyStr = ''
req.on('data', chunk => { // 服務(wù)端怎么去識(shí)別“流",并接收數(shù)據(jù)
  //chunk 即“流”的每一段數(shù)據(jù)
  bodyStr = bodyStr + chunk.toString()
})
req.on('end', () => { //服務(wù)端怎么知道流完了
  if (reqType === 'application/json') { //json 格式
    const body = JSON.stringify(bodyStr)
    console.log('body is ', body) //對(duì)象格式
   }
  res.end('接收完成') //異步
})

return
}
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容