Express路由

本文详细介绍了Express.js框架的路由使用,包括基本路由、路由方法(GET, POST)、特殊路由方法`app.all()`、路由路径匹配(字符串、模式、正则)以及路径参数的捕获和限制。此外,还讲解了路由处理程序的多种方式,如单个回调、多个回调和回调函数数组。最后,提到了响应方法,但具体内容需参考官方文档。
摘要由CSDN通过智能技术生成

目录 

1.基本路由示例

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('hello world')
})

2.路由方法

常见的get和post方法

// GET method route
app.get('/', function (req, res) {
  res.send('GET request to the homepage')
})

// POST method route
app.post('/', function (req, res) {
  res.send('POST request to the homepage')
}

一种特殊的路由方法,app.all()用于为所有HTTP请求方法的路径加载中间件功能

app.all('/secret', function (req, res, next) {
  console.log('Accessing the secret section ...')
  next() 
})

3.路由路径

路由路径和请求方法结合,定义了可以进行请求的端点。路由路径可以是字符串、字符串模式或正则表达式。

请求路径匹配到/random.text

app.get('/random.text', function (req, res) {
  res.send('random.text')
})

请求路径匹配acd和abcd

app.get('/ab?cd', function (req, res) {
  res.send('ab?cd')
})

请求路径匹配abcdabxcdabRANDOMcdab123cd等等

app.get('/ab*cd', function (req, res) {
  res.send('ab*cd')
})

请求路径匹配 /abe 和 /abcde

app.get('/ab(cd)?e', function (req, res) {
  res.send('ab(cd)?e')
})

请求路径匹配带有a的任何路径

app.get(/a/, function (req, res) {
  res.send('/a/')
})

请求路径匹配 butterfly 和 dragonfly, 但是不匹配 butterflymandragonflyman等等

app.get(/.*fly$/, function (req, res) {
  res.send('/.*fly$/')
})

4.路径参数 

路径参数被命名为URL段,用于捕获URL在其位置处指定的值。捕获的值将填充到req.params对象中

Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

符号-和.可以对路径参数进行分割 

Route path: /flights/:from-:to
Request URL: http://localhost:3000/flights/LAX-SFO
req.params: { "from": "LAX", "to": "SFO" }
Route path: /plantae/:genus.:species
Request URL: http://localhost:3000/plantae/Prunus.persica
req.params: { "genus": "Prunus", "species": "persica" }

如果想对url中的路径参数进行限制,可以在后面加个(正则表达式)

由于正则表达式通常是文字字符串的一部分,所以用\对所有字符转义

Route path: /user/:userId(\\d+)
Request URL: http://localhost:3000/user/42
req.params: {"userId": "42"}

5.路由处理程序

单个回调函数处理路由

app.get('/example/a', function (req, res) {
  res.send('Hello from A!')
})

多个回调函数处理路由

app.get('/example/b', function (req, res, next) {
  console.log('the response will be sent by the next function ...')
  next()
}, function (req, res) {
  res.send('Hello from B!')
})

回调函数数组可以处理路由

var cb0 = function (req, res, next) {
  console.log('CB0')
  next()
}

var cb1 = function (req, res, next) {
  console.log('CB1')
  next()
}

var cb2 = function (req, res) {
  res.send('Hello from C!')
}

app.get('/example/c', [cb0, cb1, cb2])

独立功能和功能数组的组合来处理路由 

var cb0 = function (req, res, next) {
  console.log('CB0')
  next()
}

var cb1 = function (req, res, next) {
  console.log('CB1')
  next()
}

app.get('/example/d', [cb0, cb1], function (req, res, next) {
  console.log('the response will be sent by the next function ...')
  next()
}, function (req, res) {
  res.send('Hello from D!')
})

6.响应方法

后面具体看官网

https://www.expressjs.com.cn/guide/routing.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漂流の少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值