1. 路由路径的匹配

1.1 字符串匹配模式
  1. // 1. 固定匹配
  2. /*
  3. /index 可以匹配成功
  4. /index/ 可以匹配成功
  5. /index/a 不可以匹配
  6. */
  7. app.get("/index", (req,res) => {})
  8. // 2. 匹配所有路径
  9. app.get("*", (req,res) => {})
  10. // 3. 匹配以/index开头的路径
  11. /*
  12. /index 可以匹配成功
  13. /index123 可以匹配成功
  14. /index/a/b 可以匹配成功
  15. */
  16. app.get("/index*", (req,res) => {})
  17. // 4. 匹配以 /index/ 开头的路径
  18. /*
  19. /index 不能匹配成功
  20. /index/ 可以匹配成功
  21. /index/a/b 可以匹配成功
  22. */
  23. app.get("/index/*", (req,res) => {})
  24. // 5. ? 前面的字符或组出现0~1 次
  25. /*
  26. /abc 可以匹配成功
  27. /ac 可以匹配成功
  28. */
  29. app.get("/ab?c", (req,res) => {})
  30. // 6. + 前面的字符或组出现1~多 次
  31. /*
  32. /abc 可以匹配成功
  33. /abbbc 可以匹配成功
  34. /ac 不能匹配成功
  35. */
  36. app.get("/ab+c", (req,res) => {})
  37. // 7. * 表示任意个数的任意字符
  38. /*
  39. /abc 可以匹配成功
  40. /ab123c 可以匹配成功
  41. */
  42. app.get("/ab*c", (req,res) => {})
  43. // 8. ()里的内容为一组
  44. /*
  45. /abcd 可以匹配成功
  46. /ad 可以匹配成功
  47. /abd 不能匹配成功
  48. /acd 不能匹配成功
  49. */
  50. app.get("/a(bc)?d", (req,res) => {})
1.2 正则匹配模式
  1. //1. 包含index的路径
  2. /*
  3. /index 可以匹配
  4. /aindexb 可以匹配成功
  5. /a/index/b 可以匹配成功
  6. */
  7. app.get(/index/, (req,res) => {})
  8. // 2. 以.html后缀名结尾的路径
  9. /*
  10. /index.html 可以匹配
  11. /index/a.html 可以匹配成功
  12. */
  13. app.get(/.*\.html$/, (req,res) => {})

2. 路由器的处理程序

2.1 单处理程序
  1. // 方式一
  2. app.get("/", (req,res) => {})
  3. // 方式二
  4. let handler = (req,res) => {}
  5. app.get("/", handler)
2.2 多处理程序

除非是最后一次处理,否则请不要执行res.send()等发送指令
除非是最后一次处理,否则请不要忘记next形参 与next() 指令

  1. app.get("/", (req,res,next) => {
  2. console.log("handle 1")
  3. next()
  4. },(req,res,next) => {
  5. console.log("handle 2")
  6. next()
  7. },(req,res) => {
  8. res.send("Hello wrold")
  9. })

或者可以这么写

  1. let handler1 = (req,res,next) => {
  2. console.log("handle 1")
  3. next()
  4. }
  5. let handler2 = (req,res,next) => {
  6. console.log("handle 2")
  7. next()
  8. }
  9. let handler3 = (req,res,next) => {
  10. res.send("Hello wrold")
  11. }
  12. app.get("/", [handler1,handler2,handler3])

3. express 子应用程序

express模块 每一次执行都会创建一个应用
将子应用通过主应用的use方法挂在到主应用上

4. express 路由

4.1 路由的理解

路由是指接收用户请求,处理用户数据,返回结果给用户的一套程序,
后端路由的核心是URL

  1. app.get("/",(req,res,next) => {
  2. })
4.2 express路由的使用

虽然现在可以通过app.get或app.post 定义路由了, 但是在一个项目中, 路由的数量是非常多的, 如果把所有的路由都放在同一文件中,那将是非常可怕的事情,
所以express 为了解决这个问题, 提供了模块化构建路由的方式, 我们可以根据条件将路由进行分类

express 路由使用语法

  1. app.METHOD(PARH, HANDLER)

参数了解

  1. app: express 实例
  2. METHOD: http 请求方式
  3. PATH: 请求路径
  4. HANDLER: 路由匹配后执行的函数

示例:

  1. router.get("/about.html",function(req,res){
  2. res.send("<h1>个人简介</h2>")
  3. })
4.3 Router (重点) 路由模块化

express.Router 类可以创建模块化(独立的),可以挂载的路由对象.Router对象是一个完整的中间件和路由系统,因此常称其为一个”mini-app”
需求: 创建一个student路由模块,接受所有 student目录下的所有请求,响应数据

  1. 创建student模块
  1. 编写路由模块的代码
  1. // 1. 引入模块
  2. const express = require("express")
  3. // 2. 实例化路由对象
  4. const router = express.Router();
  5. // 3. 编写路由线路挂载到路由对象上
  6. router.get("/first.html", function (req, res) {
  7. res.send("<h2>一年级</2>")
  8. });
  9. router.get("/second.html", function (req, res) {
  10. res.send("<h2>二年级</2>")
  11. })
  12. // 4. 暴露对象
  13. module.exports = router;
  1. 将编写好的路由模块引入到主模块,由主模块分配对应的请求到该模块去处理
  1. // 1. 引入vip路由模块
  2. var studentRouter = require('./routes/student');
  3. // 2. 分配student目录下的路由给studentRouter模块
  4. app.use('/student', studentRouter);