nodejs(6)express学习
生活随笔
收集整理的這篇文章主要介紹了
nodejs(6)express学习
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.簡(jiǎn)單認(rèn)識(shí)express
express::一個(gè)快速的網(wǎng)站開發(fā)框架,封裝了原生的http模塊,用起來更方便;API更人性化
特點(diǎn)
基于Node.js平臺(tái)之上,進(jìn)一步封裝了 http 模塊,從而提供了更好用,更友好的 API
Express 并沒有覆蓋 原生 http 模塊中的方法,而是基于 原生方法之上,做了更友好的封裝,讓用戶體驗(yàn)更好
創(chuàng)建服務(wù)器
?
?
// npm install express -S const express = require('express')// 創(chuàng)建服務(wù)器 const app = express()// 監(jiān)聽客戶端的請(qǐng)求 // 只有客戶端的請(qǐng)求類型是 get,并且 請(qǐng)求地址是 / 根路徑的時(shí)候, // 才會(huì)調(diào)用 后面指定的處理函數(shù) app.get('/', (req, res) => {// express 中,封裝了更好用的 res.send 方法res.send('你好,express 服務(wù)器!') })// 監(jiān)聽客戶端的post請(qǐng)求,并且請(qǐng)求的地址是 /adduser 的時(shí)候, // 才會(huì)調(diào)用后面指定的處理函數(shù) app.post('/adduser', (req, res) => {res.send('服務(wù)器處理成功!') })// 啟動(dòng)服務(wù)器 app.listen(4444, () => {console.log('express server running at http://127.0.0.1:4444') })?
-
支持 發(fā)送 字符串?Content-Type: text/html;
-
支持 發(fā)送 對(duì)象 或 數(shù)組?Content-Type: application/json
-
支持 發(fā)送 Buffer 此時(shí)會(huì)當(dāng)作文件下載;
res.sendFile()
-
用法1:res.sendFile(path.join(__dirname, './view/index.html'))
-
用法2:res.sendFile('./view/movie.html', { root: __dirname })
-
注意:res.sendFile() 可以向?yàn)g覽器發(fā)送 靜態(tài)頁面;
使用res.sendFile發(fā)送文件
// 導(dǎo)入 express 模塊 const express = require('express') const path = require('path') // 創(chuàng)建 express 的服務(wù)器實(shí)例 const app = express()app.get('/', (req, res) => {// 向客戶端發(fā)送文件// sendFile 必須發(fā)送絕對(duì)路徑,或提供 root 選項(xiàng)// res.sendFile(path.join(__dirname, './views/movie.html'))// 或者// res.sendFile('./views/movie.html', { root: __dirname })// 實(shí)現(xiàn)下載功能,使用sendFIle// res.sendFile(path.join(__dirname, './New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3'))const filename = encodeURI('歌曲.mp3')res.sendFile('./New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3', { root: __dirname, headers: { 'Content-Disposition': 'attachment; filename=' + filename } })// res.download('./New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3', 'aaa.mp3', err => {// if (err) return console.log('文件下載失敗')// console.log('文件下載成功')// }) })// 調(diào)用 app.listen 方法,指定端口號(hào)并啟動(dòng)web服務(wù)器 app.listen(3001, function() {console.log('Express server running at http://127.0.0.1:3001') })?
向客戶端發(fā)送文件
const express = require('express') const path = require('path')const app = express()app.get('/', (req, res) => {// res.sendFile('直接傳遞一個(gè)絕對(duì)路徑')// res.sendFile(path.join(__dirname, './views/movie.html'))/* const name = encodeURI('一首歌曲.flac')res.sendFile('./蘇醒 - Stand Up Again.flac', {root: __dirname,headers: {'Content-Disposition': 'attachment; filename=' + name}}) */res.download('./New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3', 'Battle.mp3', err => {if (err) return console.log('文件下載失敗!')console.log('下載完成!')}) })app.listen(3001, () => {console.log('server running at http://127.0.0.1:3001') })?
點(diǎn)擊下載歌曲
const express = require('express')const app = express()app.get('/', (req, res) => {res.sendFile('./music.html', { root: __dirname }) })// 監(jiān)聽客戶端的下載請(qǐng)求,返回一個(gè)具體的文件 app.get('/dowload/music', (req, res) => {res.download('./New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3', 'Battle Angel.mp3', err => {if (err) return console.log('失敗了!')console.log('ok')}) })app.listen(3001, () => {console.log('server running at http://127.0.0.1:3001') })music.html
<a href="/dowload/music">下載音樂</a>?
使用express.static快速托管靜態(tài)資源
const express = require('express')const app = express()//#region 注釋 /* app.get('/', (req, res) => {res.sendFile('./views/home.html', { root: __dirname }) })app.get('/movie.html', (req, res) => {res.sendFile('./views/movie.html', { root: __dirname }) })app.get('/about.html', (req, res) => {res.sendFile('./views/about.html', { root: __dirname }) }) */ //#endregion// 使用 app.use 來進(jìn)行相關(guān)的配置 // app.use(express.static('./views'))// 步驟的拆解 const result = express.static('./views') app.use(result) // 再次托管一下樣式表的資源目錄 app.use('/css', express.static('./css')) // 托管JS文件目錄 app.use('/js', express.static('./js'))// vue 打好的包,直接放在一個(gè)文件夾下 // 然后app.use(express.static('./views'))使用就可以了 app.listen(3001, () => {console.log('server running at http://127.0.0.1:3001') })?
轉(zhuǎn)載于:https://www.cnblogs.com/houfee/p/10364564.html
總結(jié)
以上是生活随笔為你收集整理的nodejs(6)express学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 虚拟化-虚机安装
- 下一篇: UE4游戏开发基础命令