1.简单认识express
express::一个快速的网站开发框架,封装了原生的http模块,用起来更方便;API更人性化
特点
基于Node.js平台之上,进一步封装了
http
模块,从而提供了更好用,更友好的 API使用Express创建网站,比使用原生的http模块更加方便;
Express 并没有覆盖 原生 http 模块中的方法,而是基于 原生方法之上,做了更友好的封装,让用户体验更好
创建服务器
// npm install express -Sconst express = require(‘express‘)// 创建服务器const app = express()// 监听客户端的请求// 只有客户端的请求类型是 get,并且 请求地址是 / 根路径的时候,// 才会调用 后面指定的处理函数app.get(‘/‘, (req, res) => { ?// express 中,封装了更好用的 res.send 方法 ?res.send(‘你好,express 服务器!‘)})// 监听客户端的post请求,并且请求的地址是 /adduser 的时候,// 才会调用后面指定的处理函数app.post(‘/adduser‘, (req, res) => { ?res.send(‘服务器处理成功!‘)})// 启动服务器app.listen(4444, () => { ?console.log(‘express server running at http://127.0.0.1:4444‘)})
express 中的快捷方法
res.send()
支持 发送 字符串
Content-Type: text/html;
支持 发送 对象 或 数组
Content-Type: application/json
支持 发送 Buffer 此时会当作文件下载;
const fs = require(‘fs‘)const path = require(‘path‘)// 导入 express 模块const express = require(‘express‘)// 创建 express 的服务器实例const app = express()app.get(‘/‘, (req, res) => { ?// 1.发送普通文本 ?// res.send(‘你好‘) ?// 2.发送对象或数组 ?// res.send({ name: ‘zs‘, age: 30 }) ?// res.send([‘zs‘, ‘ls‘, ‘zl‘]) ?// 3.发送Buffer二进制 ??1. 得到二进制 ?2. 使用send发送二进制 ?fs.readFile(path.join(__dirname, ‘./小明.mp3‘), (err, buf) => { ???if (err) return res.send(‘发送文件失败!‘) ???// 发送 Buffer 二进制 ???res.send(buf) ?})})// 调用 app.listen 方法,指定端口号并启动web服务器app.listen(3001, function() { ?console.log(‘Express server running at http://127.0.0.1:3001‘)})
res.sendFile()
用法1:
res.sendFile(path.join(__dirname, ‘./view/index.html‘))
用法2:
res.sendFile(‘./view/movie.html‘, { root: __dirname })
注意:
res.sendFile()
可以向浏览器发送 静态页面;
使用res.sendFile发送文件
// 导入 express 模块const express = require(‘express‘)const path = require(‘path‘)// 创建 express 的服务器实例const app = express()app.get(‘/‘, (req, res) => { ?// 向客户端发送文件 ?// sendFile 必须发送绝对路径,或提供 root 选项 ?// res.sendFile(path.join(__dirname, ‘./views/movie.html‘)) ?// 或者 ?// res.sendFile(‘./views/movie.html‘, { root: __dirname }) ?// 实现下载功能,使用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(‘文件下载成功‘) ?// })})// 调用 app.listen 方法,指定端口号并启动web服务器app.listen(3001, function() { ?console.log(‘Express server running at http://127.0.0.1:3001‘)})
向客户端发送文件
const express = require(‘express‘)const path = require(‘path‘)const app = express()app.get(‘/‘, (req, res) => { ?// res.sendFile(‘直接传递一个绝对路径‘) ?// 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‘)})
点击下载歌曲
const express = require(‘express‘)const app = express()app.get(‘/‘, (req, res) => { ?res.sendFile(‘./music.html‘, { root: __dirname })})// 监听客户端的下载请求,返回一个具体的文件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快速托管静态资源
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 来进行相关的配置// 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 打好的包,直接放在一个文件夹下// 然后app.use(express.static(‘./views‘))使用就可以了app.listen(3001, () => { ?console.log(‘server running at http://127.0.0.1:3001‘)})
nodejs(6)express学习
原文地址:https://www.cnblogs.com/houfee/p/10364564.html