目录
Express介绍

测试最基本的服务器
- // 1.导入express
- const express=require('express')
-
- // 2. 创建web服务器
- const app=express()
-
- // 3. 启动web服务器
- app.listen(8080,()=>{
- console.log('express server running at http://127.0.0.1:8080');
- })
Express基本使用
1.我们可以通过req.query得到客户端发送过来的查询参数
2.可以通过req.params得到url的动态参数
- //:id为动态参数
- app.get('/user/:id',(req,res){
- console.log(req.params);
- res.send(req.params);
- })

- // 1.导入express
- const express=require('express')
-
- // 2. 创建web服务器
- const app=express()
-
- // 4.监听客户端的GET和POST请求,并向客户端响应具体的内容
- app.get('/',(req,res)=>{
- // 调用express提供的res.send()方法,向客户端发送 JSON 对象
- res.send({
- name:'zs',
- age:20,
- gender:'男'
- })
-
- })
-
- app.post('/',(req,res)=>{
- // 调用express提供的res.send()方法,向客户端响应一个文本字符串
- res.send('请求成功')
- })
-
- // 获取url中携带的查询参数
-
- app.get('/',(req,res)=>{
- /*
- 通过req.query可以获取到客户端发送过来的 查询参数
- 注意:默认情况下,req.query是一个空对象
- */
-
- console.log(req.query);
- res.send(req.query)
- })
-
- /*
- 获取url中的动态参数,可以有多个动态参数
- 注意:这里的:id等是一个动态参数
- */
-
- app.get('/user/:id/:username',(req,res)=>{
- // req.params 是动态匹配到的URL参数,默认是一个空对象
- console.log(req.params);
- res.send(req.params)
-
- })
-
-
- // 3. 启动web服务器
- app.listen(8080,()=>{
- console.log('express server running at http://127.0.0.1:8080');
- })
托管静态资源
app.use(express.static('静态资源目录'))

- const express=require('express')
- const app=express()
-
- //1.快速对外调用提供的外部资源
- app.use(express.static('./clock'))
-
- app.listen(80,()=>{
- console.log('express server running at http://127.0.0.1')
- })


- const express=require('express')
- const app=express()
-
- //1.快速对外调用提供的外部资源
- app.use('/hearts',express.static('./clock'))
-
- app.listen(80,()=>{
- console.log('express server running at http://127.0.0.1')
- })
nodemon
nodemon能够监听项目文件的变动,帮助自动重启项目(热部署)

1.全局安装nodemon
npm i -g nodemon
2.启动项目:(能够帮忙自动重启项目,相当于一个守护线程起到热部署的效果)
nodemon xx.js
路由

2.Express中的路由


3.路由的匹配过程

- const express=require('express')
-
- const app=express()
-
- //将路由挂载到app上
- app.get('/',(req,res)=>{
- res.send('hello word')
- })
-
- app.post('/',(req,res)=>{
- res.send('Post request')
- })
-
- app.listen(8080,()=>{
- console.log('http://127.0.0.1:8080');
- })
模块化路由

1.流程:将express创建的服务器app——>app进行监听但是并不挂载路由——>我们再创建一个路由模块,然后向外暴露即可(express.Router())
- // 这是路由模块
-
- // 1.导入express
- const express=require('express')
-
- // 2.创建路由对象
- const router=express.Router()
-
- // 3.挂在具体的路由
- router.get('/',(req,res)=>{
- res.send('Get user list')
- })
- router.post('/',(req,res)=>{
- res.send('Add new user')
- })
-
- // 4.向外导出路由对象
- module.exports=router
利用app.use()使用路由模块,这个路由就能正常生效
app.use():注册全局中间件,第一个参数可以为全局统一的访问路径
- const express=require('express')
- const app=express()
-
- // 1.导入路由模块
- const router=require('./03_router.js')
- // 2.注册路由模块 app.use()函数用来注册全局中间件
- app.use('/api',router)
-
- app.listen(8080,()=>{
- console.log('http://127.0.0.1:8080');
- })
中间件的概念

1.next函数的作用链式调用,实现多个中间件连续调用,把流转关系给到下一个中间件

- const express=require('express')
- const app=express()
-
- // 定义第一个全局中间件
- app.use((res,req,next)=>{
- console.log('调用了第一个全局中间件');
- next()
- })
-
-
- // 定义第二个全局中间件
- app.use((res,req,next)=>{
- console.log('调用了第二个全局中间件');
- next()
- })
-
- app.get('/',(req,res)=>{
- res.send('User page')
- })
-
- app.listen(8080,()=>{
- console.log('http://127.0.0.1:8080');
- })

3.中间件的作用


- const express=require('express')
- const app = express()
-
- app.use((req,res,next)=>{
- console.log('调用第一个全局中间件')
- next()
- })
- app.use((req,res,next)=>{
- console.log('调用第两个全局中间件')
- next()
- })
- app.get('/user',(req,res)=>{
- res.send('User page')
- })
- app.listen(8080,()=>{
- console.log('http://127.0.0.1:8080');
- })
先是将结果给到我们的中间件,然后再传递给res,app.use()是中间件全局挂载,我们可以在请求中设置中间件

- const express=require('express')
- const app=express()
-
- // 1.定义局部中间件函数
- const mw1=(req,res, next)=>{
- console.log('调用了第一个局部生效中间件');
- next()
- }
- const mw2=(req,res, next)=>{
- console.log('调用了第二个局部生效中间件');
- next()
- }
-
- // 2.创建路由
- // mw1只在第一个app.get()中生效
- app.get('/',mw1,mw2,(req,res)=>{
- res.send('home page')
- })
-
- app.get('/user',(req,res)=>{
- res.send('user page')
- })
-
- app.listen(8080,()=>{
- console.log('http://127.0.0.1:8080');
- })
6.定义多个局部中间件

中间件的分类


- // 这是路由模块
-
- // 1.导入express
- const express=require('express')
-
- // 2.创建路由对象
- const router=express.Router()
-
- // 3.挂在具体的路由
- router.get('/',(req,res)=>{
- res.send('Get user list')
- })
- router.post('/',(req,res)=>{
- res.send('Add new user')
- })
-
- // 4.向外导出路由对象
- module.exports=router
- const express=require('express')
- const app=express()
-
- // 1.导入路由模块
- const router=require('./03_router.js')
- // 2.注册路由模块 app.use()函数用来注册全局中间件
- app.use('/api',router)
-
- app.listen(8080,()=>{
- console.log('http://127.0.0.1:8080');
- })
利用中间件对错误进行拦截——>下面定义了一个全局中间件作为错误的拦截,发生错误就进入中间件进行捕获
注意错误级别的中间件需要放在所有路由之后
- const express=require('express')
- const app=express()
-
- // 定义路由
- app.get('/',(req,res)=>{
- // 人为制造错误
- throw new Error('人为制造错误')
- res.send('homepage')
- })
-
- // 错误级别中间件放在所有路由之后
- // 定义错误级别中间件,捕获整个项目的异常错误,防止程序的崩溃
- app.use((err,req,res,next)=>{
- console.log('发生了错误'+err.message);
- res.send('error:'+err.message)
- })
-
- app.listen(8080,()=>{
- console.log('http://127.0.0.1:8080');
- })

在服务器我们可以用request.body去接收客户端传来的请求体数据

需要配置一个express.json的中间件进行解析请求的json数据
- const express=require('express')
- const app=express()
- //通过express.json()解析表单中的JSON格式数据
- app.use(express.json)
- app.post('/user',(req,res)=>{
- //接收客户端传来的请求体数据
- console.log(req,body)
- res.send('OK')
- })
例子2:定义express.urlencoded()中间件,来解析表单中url-encoded格式的数据

例子3:解析表单对象

自定义中间件
- //1.导入模块
- const express=require('express')
- const app=express()
- //2.逻辑
- app.use((req,res,next)=>{
- // 定义具体业务逻辑
- let str=''
- //监听req的data
- req.on('data',(chunk)=>{
- str+=chunk
- })
- //监听req的end事件
- req.on('end',()=>{
- console.log(str)
- })
- })
-
-
-
- //3.指定端口号并且启动web服务器
- app.listen(80,()=>{
- console.log();
- })
使用express内置的模块querystring——>parse()将请求体数据解析为对象格式

- // 导致内置的querystring模块
- const qs=require('querystring')
-
- const bodyParser=(req,res,next)=>{
- // 1.定义str字符串,用来存储客户端发送过来的数据
- let str=''
- // 2.监听req的data时间
- req.on('data',(chunk)=>{
- str += chunk
- })
-
- // 3.监听req对象的end事件(请求体发送完毕后自动触发)
- req.on('end',()=>{
- // 在str中存放的是完整的请求体数据据
- // console.log(str);
- // 把字符串格式的请求体数据,解析成为对象格式
- const body=qs.parse(str)
- req.body=body
- next()
- })
- }
-
- module.exports=bodyParser
引入自己封装的中间件模块
- const express = require("express");
- const app=express()
- // 1.导入自己封装的中间件模块
- const customBodyParser=require('./10_拆分自定义中间件')
- // 2.将自定义的中间件函数,注册为全局可用的中间件
- app.use(customBodyParser)
-
- app.get('/',(req,res)=>{
- res.send(req.body)
- })
-
- app.listen(8080,()=>{
- console.log('Express server running st http://127.0.0.1:8080');
- })