1. 路由

1. 初始化

  1. const express = require('express')
  2. const app = express()
  3. app.listen(9999)

2. 常规路由

  1. app.get('/', (req, res) => {
  2. const queryParams = req.query
  3. })
  4. app.post('/todo', (req, res) => {
  5. })
  6. app.delete('/text', (req, res) => {
  7. })
  8. app.put('/text', (req, res) => {
  9. })

3. 动态路由

路由匹配是按从上到下来的,匹配到一个就不会再继续匹配,因此,动态路由需要注意顺序

  1. app.get('/article/:id', (req, res) => {
  2. const id = req.params['id']
  3. })
  4. // 访问/article/add会匹配到/article/:id中,不会进入/article/add路由中
  5. app.get('/article/add', (req, res) => {
  6. })