為什么我們要學習Node.js?
-
認為:
- Node.js就學習一周,時間比較短,不重要
- 將來作業我后端又不用Node.js做,我們又java/python/php/c,為什么要在意它
- Node.js接下來專案中又不用,不用好好學,不用在意
-
重要性?
- Node.js可以說是對js功能的擴展,比如: Node.js可以操作檔案,資料庫
- Node.js是webpack的底層語言,如果Node.js不了解,那么我們webpack學不好
- Node.js是現在vue/React中構建專案的一環,如果學不好,那么會導致Vue/React基礎不好
- Node.js可以充當前后端連接的一個橋梁
- Node.js也可以當做后端這個角色來看,如果我們想和后端配合的更好,那么我們必須了解后端,Node.js可以說是我們接觸后端的一個方式
- Node.js現在已經成為很多企業的職位要求了
-
Node.js 是一個基于Chrome V8 引擎的JavaScript運行環境
- 服務端的js有什么能力?(服務器)
-
Node.js使用了一個事件驅動、非阻塞式I/O的模型,使其輕量又高效
- Node.js功能
- 事件驅動
- 非阻塞I/O模型【 異步的輸入輸出,比如: 檔案操作、資料庫操作 等 】
- Node.js功能
Node.js檔案的運行?
- 使用Node.js編譯器
- 使用命令運行
Node.js檔案的實時監聽改動
- nodemon
Node.js版本
- Current [ 最新的版本 ] - 小白鼠
- LTS - 長期穩定版本
Node.js的模塊化問題
- Node.js采用了Common.js模塊化
- 應用
- 內置模塊 -> 可以直接使用
const fs = require('fs') // Common.js 模塊引入方式
/*
* fs 是一個物件
* 定義的fs 其實拿的是一個地址,地址我們希望是穩定不變的,所以const
*/
// fs.readFileSync // 同步讀
// fs.readFile // 異步讀
- 第三方模塊 -> Node.js沒有的 類似于插件
/*
* 類似插件
* 我們現在想在我們的當前檔案中引入帶有功能的插件
* 前端第三方模塊全部都在一個網站中: www.npmjs.com
* 使用
* 1. 安裝【 npm/cnpm/yarn 】
* 2. cnpm i request -S/-D
* -S 生產環境
* -D 開發環境
*
* 問題: 這里是否存在跨域?
* 不存在 Node.js是運行在服務端的,不是瀏覽器端,沒有同源策略
*
! 總結
! 第三方模塊使用? 將來別人專案中使用了你沒有用過的東西,怎么辦?
! 1. npm.js 查閱檔案
! 2. 先寫單案例測驗
! 3. 記錄使用檔案,記錄自己博客中
*/
const request = require('request')
request('https://m.lagou.com/listmore.json', function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
});
- 自定義模塊
/*
* 自定義模塊
* 自己創建模塊,自己使用
* 型別:
* 1. 函式
* 2. 物件
* 3. 字串
*/
// 1. 定義模塊
const people = {
name: '西閣',
sex: 'man',
age: 18
}
// 2. 匯出模塊
module.exports = people
// 3. 自定義模塊匯入
const people = require('./3-自定義模塊定義.js')
console.log('people.name:', people.name )
-
模塊化解決的是什么問題?
- js中可以參考其他型別檔案(html css sass )
-
自定義模塊上傳實作步驟 【 公司中應用比較多 】
-
- 創建檔案夾,注意命名不要沖突
-
- 創建package.json檔案
- npm init / npm init -y
-
- 創建了 index.js, 里面封裝任意一個功能
-
- 創建一個 npm.js 賬號
- 發送一個郵箱連接激活【 手動點 】
-
- 保證我們當前的源是 npm 源
- $ nrm use npm
-
- 登錄賬號
- $ npm adduser
-
- 上傳
- $ npm publish
-
JSON.stringify / JSON.parse
- 字串、物件互相轉換
- 拷貝
const fs = require('fs')
const data = https://www.cnblogs.com/moon3/p/fs.readFileSync('./data.json','utf8')//字串
const newData = https://www.cnblogs.com/moon3/p/JSON.parse( data )//將JSON字串轉為一個物件
const newStr = JSON.stringify( newData )//將 JavaScript 物件轉換為 JSON 字串
const state = {
msg:'千鋒教育',
obj: {
x: 1,
y: 2
}
}
// 深拷貝 -> 1. 遞回 2. JSON序列化實作
const newState = JSON.parse(JSON.stringify( state ))
newState.msg = " hello Node.js "
console.log('state',state)
console.log('newState',newState)
querystring 內置模塊
- 使用場景
- 用于處理url上的查找字串
/*
! querystring - 應用場景: 處理url查找字串
! 1. querystring.parse string -> object
! 2. querystring.stringify object -> string
! 3. querystring.escape 中文轉碼
! 4. querystring.unescape 中文解碼
*/
const qs = require('querystring')
const url = require('url')
// console.log("西閣: qs", qs)
// ! 1. parse
const str = 'https://detail.tmall.com/item.htm?spm=a230r.1.14.6.7a344d82XrCvx0&id=604098442154&cm_id=140105335569ed55e27b&abbucket=2'
const newObj = qs.parse(url.parse( str ).query,'&','=')
// console.log("西閣: newObj", newObj)
/*
{
spm: 'a230r.1.14.6.7a344d82XrCvx0',
id: '604098442154',
cm_id: '140105335569ed55e27b',
abbucket: '2'
}
*/
// ! 2. stringify
const newStr = qs.stringify( newObj )
// console.log("西閣: newStr", newStr)
// ! 3. escape
const str1 = 'city=北京'
const city = qs.escape( str1 )
console.log("西閣: city", city) // city%3D%E5%8C%97%E4%BA%AC
// !4. unescape
const cityCape = qs.unescape( city )
console.log("西閣: cityCape", cityCape)
path
- 使用場景
- 用于處理絕對路徑/磁盤路徑
- 問題: 瀏覽器中全域物件是?Node.js全域變數?
- 瀏覽器中全域物件: window
- Node.js全域變數: global
const path = require('path')
console.log("西閣: path", path)
// console.log( __dirname ) // 全域變數
/* e:\1911\1-Node.js\day01\code\5-內置模塊 */
// const pathUrl = path.join( __dirname, 'aa')
const pathUrl = path.resolve( __dirname, 'aa')
console.log("西閣: pathUrl", pathUrl)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/125530.html
標籤:其他
