1.简介
使用Express,可以方便快捷创建web网站服务器或API接口服务器。
2.基本用法
安装npm i express@4.17.1
2.创建基本web服务器
const express = require('express')
const app=express()
app.listen(80,()=>{
console.log('express server running at http://127.0.0.1')
})
3.get 与 post 请求 并用send方法相应客户端
const express = require('express')
const app=express()
//监听GET POST请求 并向客户端响应具体内容
app.get('/user',(req,res)=>{
res.send({name:'wcn',age:20,gender:'男'})
})
app.post('/user',(req,res)=>{
res.send('请求成功!')
})
app.listen(80,()=>{
console.log('express server running at http://127.0.0.1')
})
4.通过req.query对象,可以访问到客户端通过查询字符串形式,发送到服务器的参数
app.get('/',(req,res) =>{
console.log(req.query)
//通过req.query可以获取到客户端发过来的 查询参数
//req.query默认是一个空对象
res.send(req.query)
})
5.获取URL中的动态参数
app.get('/user/:id',(req,res)=>{
console.log(req.params)
res.send(req.params)
})
:id为动态参数 可以放多个动态参数
3.托管静态资源
const express = require('express')
const app = express()
//调用express.static()方法,快速对外提供静态资源
app.use(express.static('./clock'))
app.listen(80,()=>{
console.log('express server running at http://127.0.0.1')
})
调用express.static()方法,可以创建一个静态资源服务器,括号里的文件都对外开放。
托管多个静态资源目录
app.use(express.static('./files'))
app.use(express.static('./clock'))
需要多次调用express.static(),且会根据顺序查找所需文件
http://127.0.0.1/index.html就会访问files里的index.html
挂载路径前缀
app.use('/hhh',express.static('./files'))
app.use(express.static('./clock'))
http://127.0.0.1/hhh/index.html访问files里面的index.html
4.nodemon
当项目代码被修改后nodemon可以监听项目文件的变动,并自动帮我们重启项目,不用手动重启,方便我们开发与调式
npm i -g nodemon //安装nodemon -g全局安装
使用方式
nodemon 文件名
Express路由
路由——映射关系
在Express中,路由指 客户端请求 和 处理函数之间的映射关系。
Express中路由由三部分组成 请求类型、请求的URL地址、处理函数。
1.路由匹配
请求类型和请求的URL同时匹配成功,Express会将这次请求交给对应的function处理。
路由匹配注意点:
1、按照先后顺序匹配。
2、请求的类型和URL要匹配成功,不然无法调用处理函数。
最简单的用法
const express = require('express')
const app = express()
//挂载路由
app.get('/',(req,res)=>{
res.send('hello world')
})
app.post('/',(req,res)=>{
res.send('Post request.')
})
app.listen(80,()=>{
console.log('http://127.0.0.1');
})
即把路由挂载到app上。
2.路由的使用
为了方便对路由模块化管理,推荐将路由抽离为单独的模块。
1.模块化
const express = require('express')
const app = express()
//1.导入路由模块
const router = require('地址')
//2.注册路由模块
app.use(router)
app.listen(80,()=>{
console.log('http://127.0.0.1')
})
注意:导入文件必须要用module.exports导出。
2.添加前缀
app.use('/api',router)
3.Express中间件
1.概念
业务过程处理中的中间处理环节。
next函数的作用
把流转关系交给下一个中间件或路由
2.定义一个中间件函数
const express = require('express')
const app = express()
const mw=function(req,res,next){
console.log('这是最简单的中间件函数')
next()
}
app.listen(80,()=>{
console.log('httpl://127.0.0.1')
})
3.全局生效的中间件
客户端发起的任何请求,达到服务器都会触发中间件,即全局生效中间件
//将mw注册全局生效
app.use(mw)
简化形式
app.use((req,res,next)=>{
console.log('这是最简单的中间件')
next()
})
4.中间件的作用
中间件共享同一份req,res,我们可以在中间件中统一为req,res添加自定义的属性或方法,供下游的中间件或路由使用。
5.定义多个中间件
可以连续使用app.use()连续定义多个全局中间件,客户端请求到达服务器中间件会按照顺序依次调用。
6.局部生效的中间件
不用app.use()的中间件
const mw1 = (res,req,next)=>{
console.log('调用了局部生效的中间件')
next()
}
//将要使用的中间件放入
app.get('/',mw1,(req,res)=>{
res.send('Home')
})
7.使用中间件注意事项
1、先定义中间件再定义路由,从上到下匹配
2、可连续调用多个中间件
3、别忘了next()函数
4、防止逻辑混乱,next()后不要再写额外代码
5、连续调用多个中间件,会共享req res对象
中间件的分类
1.应用级别的中间件
通过app.use() app.get() app.post() 绑定到app实例上的中间件。
2.路由级别中间件
绑定到express.Router()实例上的中间件
3.错误级别中间件
4个形参(err,req,res,next)
注意点:错误级别中间件必须注册再所有路由之后!
4.Express内置中间件
express.static 快速托管静态资源的内置中间件
express.json解析JSON格式的请求体数据
express.urlencoded解析URL-encoded格式的请求数据
5.第三方的中间件
1、运行npm install body-parser安装中间件
2、使用require导入中间件
3、调用app.use()注册并使用中间件
const parser = require('body-parser')
app.use(parser.urlencoded({extended:false}))