node.js学习笔记
生活随笔
收集整理的這篇文章主要介紹了
node.js学习笔记
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
# node.js學(xué)習(xí)筆記標(biāo)簽(空格分隔): node.js---## 一 內(nèi)置模塊學(xué)習(xí)
### 1. http 模塊
```
//1 導(dǎo)入http模塊
const http =require('http')
//2 通過(guò)http模塊的createServer方法創(chuàng)建服務(wù)器
let server = http.createServer()
//3綁定監(jiān)聽(tīng)請(qǐng)求事件
server.on('request',function(req,res){ //第一個(gè)參數(shù)是請(qǐng)求,第二個(gè)是響應(yīng)if(req.url==='/'){//res返回結(jié)果可以用res.write()但是最后必須接上一個(gè)res.end(),只有接收到end后瀏覽器才會(huì)直接拿結(jié)果走人
res.end('這是網(wǎng)站的首頁(yè)')//以上代碼等同于//res.write('首頁(yè)')//res.end()
}
})
//4 設(shè)置監(jiān)聽(tīng)端口號(hào)
server.listen(3000,function(){
console.log('server-3000 is running')//輸出說(shuō)明瀏覽器啟動(dòng)成功
}
```
### 2. fs模塊
```
//文件數(shù)據(jù)讀取
const fs = require('fs')
//參數(shù)1:文件路徑 參數(shù)2:回調(diào)函數(shù),回調(diào)函數(shù)兩個(gè)參數(shù)一個(gè)error一個(gè)data,讀取成功data為讀取內(nèi)容,err為空,失敗data為空,err為錯(cuò)誤對(duì)象
//注意:這里的data是二進(jìn)制數(shù),可以用toString方法轉(zhuǎn)為字符串后使用
fs.readFile('./data.txt',function(err,data){
if(err) return
console.log(data.toString())
})//文件數(shù)據(jù)寫(xiě)入
fs.writeFile('./data.txt','data',function(err){//注意這里的寫(xiě)入文件會(huì)覆蓋源文件中的內(nèi)容//如果指定目錄下無(wú)此文件,執(zhí)行代碼會(huì)自動(dòng)創(chuàng)建一個(gè)文件//err 錯(cuò)誤對(duì)象,寫(xiě)入成功為null
})
//文件數(shù)據(jù)追加
fs.appendFile('data.txt','data',function(err){
//這里寫(xiě)入數(shù)據(jù)會(huì)在原有的數(shù)據(jù)后面追加數(shù)據(jù),不會(huì)覆蓋,當(dāng)指定文件未找到會(huì)自動(dòng)創(chuàng)建一個(gè)該文件
})
```
### 3. path模塊
```
const path = require('path')
console.log(path.parse('/index/abc/main.html'))// { // root: '/', 根目錄// dir: '/index/abc', dirname 目錄// base: 'main.html', basename 文件名(含后綴)// ext: '.html', 文件擴(kuò)展名// name: 'main' } 不含后綴的文件名//__dirname 代表當(dāng)前文件所在目錄 join方法是在做一個(gè)路徑的拼接path.join(__dirname,'/index.html')
```## 二 node中的相關(guān)插件
### 1. nodemon
+ 作用:- 文件更改后自動(dòng)重啟服務(wù)器
+ 安裝:- 安裝到全局 npm i nodemon -g
+ 使用:- 直接在要使用的目錄中運(yùn)行命令行工具,nodemon app.js
### 2. art-template
+ 作用:- 字符串模板
+ 安裝:- npm i art-template -S
+ 使用:
```
const template = require('art-template')
let string = template(str,{data:list})//第一個(gè)參數(shù)是字符串模板 第二個(gè)參數(shù)是數(shù)據(jù)對(duì)象, 返回值是處理好的字符串
/*
字符串模板的語(yǔ)法:
{{each data}}
<li>{{$value}}-----{{$index}}</li>
{{/each}}
*/
```
### 3. express
+ 作用:- 封裝了node的http模塊的功能,使單間服務(wù)器變得更簡(jiǎn)單
+ 安裝:- 安裝到本地 npm i express -S
+ 使用:
```
const express = require('express')
const app = express()
//開(kāi)放public目錄中的靜態(tài)資源
app.use('/public/',express.static('./plublic'))
//app.get()處理get請(qǐng)求,默認(rèn)無(wú)法處理post請(qǐng)求,如需POST,必須要安裝第三方包才行
app.get('/',function(req,res){res.send('首頁(yè)')//res.redirect('/') 重定向功能
})
app.get('/index',function(req,res){res.send('還是首頁(yè)')
})
app.get('/person',function(req,res){res.send('<h1>個(gè)人中心</h1>')
})
app.get('/more',function(req,res){res.send('加載更多')
})
app.listen(3000,function(){})
```
### 4. 在express中使用 art-template 模板
+ 安裝:- npm i art-template -S- npm i express-art-template -S
+ 使用:
```
const express = require('express')const app = express()
//1. 安裝好了art-template 和 express-art-template 后,通過(guò)app.engine向response中掛載render方法,app.engine()第1個(gè)參數(shù)配置處理哪種文件,第2個(gè)參數(shù)加載art-template
app.engine('html',require('express-art-template'))app.get('/',function(req,res){let items = ['蘋(píng)果','梨子','橘子','葡萄','桃子']
// 2. 調(diào)用render()渲染index.html ,注意:這里的index.html只能放在views文件夾中,然后這里就能直接寫(xiě)文件名了,第1個(gè)參數(shù)是要渲染的文件,第2個(gè)參數(shù)要渲染的數(shù)據(jù)
// 注意:第一個(gè)參數(shù)不能寫(xiě)路徑,默認(rèn)會(huì)去項(xiàng)目中的 views 目錄查找該模板文件,Express有一個(gè)約定:開(kāi)發(fā)人員把所有的視圖文件都放到 views 目錄中res.render('index.html',{items:items})
})
```
### 5. body-parser+ 作用:- 使express可以獲取post請(qǐng)求中的數(shù)據(jù)+ 安裝:- npm i body-parser -S+ 使用:
```
//1.加載模塊
const bodyParser = require('body-parser')
//2.配置bodyParser選項(xiàng)
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
//3.配置完成后req上面就會(huì)多一個(gè)body的屬性,就可以直接調(diào)用來(lái)獲得請(qǐng)求體了
app.post('/post', function (req, res) {// req.query 只能拿 get 請(qǐng)求參數(shù)
console.log(req.body)
```
## 6. 抽離路由模塊的步驟
+ 1. 創(chuàng)建一個(gè)router.js文件
```
//載入express模塊
const express = require('express')
//創(chuàng)建一個(gè)路由容器
const router = express.Router()
//把路由掛載到路由對(duì)象上
router.get('/',function(){
})
//向外暴露路由對(duì)象
moudle.express = router
```
+ 2. 在app.js 中引用路由模塊
```
//導(dǎo)入路由模塊
const router = require('router.js')
//啟用路由模塊
app.use(router)
```
?
轉(zhuǎn)載于:https://www.cnblogs.com/superjsman/p/10049617.html
總結(jié)
以上是生活随笔為你收集整理的node.js学习笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Gluon.vision的几类数据集
- 下一篇: 团队作业--第二次