Express 路由简介

路由表示应用程序端点 (URI) 的定义以及响应客户端请求的方式。它包含一个请求方时(methods)、路径(path)和路由匹配时的函数(callback);

1
app.methods(path, callback);

Express 路由方法

Express 方法源于 HTTP 方法之一,附加到 express 类的实例。它可请求的方法包括:

get、post、put、head、delete、options、trace、copy、lock、mkcol、move、purge、propfind、proppatch、unlock、report、mkactivity、checkout、merge、m-search、notify、subscribe、unsubscribe、patch、search 和 connect。

路径

Express 路径包含三种表达形式,分别为字符串、字符串模式、正则表达式

字符串路径

1
2
3
app.get("/login",function(req,res){
res.send("heng... women");
})

此路径地址将与/login 匹配

字符串模式路径

此路由路径将与acd和相匹配abcd

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

这条路线的路径将会匹配abcdabbcdabbbcd,等等。

1
2
3
app.get('/ab+cd', function (req, res) {
res.send('ab+cd')
})

这条路线的路径将会匹配abcdabxcdabRANDOMcdab123cd,等。

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

此路由路径将与/abe和相匹配/abcde

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

正则表达式路径

此路由路径将匹配其中带有“ a”的任何内容。

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

这条路线的路径将匹配butterflydragonfly,但不butterflymandragonflyman等。

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

基础路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const express = require("express");
var app = express();

app.get("/",function(req,res){
res.send(`<h1>主页</h1>`);
});
app.get("/login",function(req,res){
res.send(“登录页面”);
});
app.get("/registe",function(req,res){
res.send(“注册页面”);
});

app.listen(8080);

输入http://127.0.0.1:8080/login和http://127.0.0.1:8080/registe都能进入不同路由。

动态路由

路线参数

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

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

要使用路由参数定义路由,只需在路由路径中指定路由参数,如下所示。

1
2
3
app.get('/users/:userId/books/:bookId', function (req, res) {
res.send(req.params)
})

路径参数的名称必须由“文字字符”([A-Za-z0-9_])组成。

由于连字符(-)和点(.)是按字面解释的,因此可以将它们与路由参数一起使用,以实现有用的目的。

1
2
3
4
5
6
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" }

要更好地控制可以由 route 参数匹配的确切字符串,可以在括号(())后面附加一个正则表达式:

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

由于正则表达式通常是文字字符串的一部分,因此请确保\使用其他反斜杠对所有字符进行转义,例如\\d+

在 Express 4.x 中,不以常规方式解释正则表达式中*字符。解决方法是使用{0,}代替*。这可能会在 Express 5 中修复。

路线处理程序

您可以提供行为类似于中间件的多个回调函数来处理请求。唯一的例外是这些回调可能会调用next('route')以绕过其余的路由回调。您可以使用此机制在路由上施加先决条件,然后在没有理由继续使用当前路由的情况下将控制权传递给后续路由。

路由处理程序可以采用函数,函数数组或二者组合的形式,如以下示例所示。

单个回调函数可以处理路由。例如:

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

多个回调函数可以处理一条路由(确保指定了next对象)。例如:

1
2
3
4
5
6
7
8
9
10
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!')
}
)

回调函数数组可以处理路由。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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])

独立功能和功能数组的组合可以处理路由。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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!')
}
)

应对方法

res下表中响应对象()上的方法可以将响应发送到客户端,并终止请求-响应周期。如果从路由处理程序中未调用这些方法,则客户端请求将被挂起。

方法 描述
res.download() 提示要下载的文件。
res.end() 结束响应过程。
res.json() 发送 JSON 响应。
res.jsonp() 发送带有 JSONP 支持的 JSON 响应。
res.redirect() 重定向请求。
res.render() 渲染视图模板。
res.send() 发送各种类型的响应。
res.sendFile() 将文件作为八位字节流发送。
res.sendStatus() 设置响应状态代码,并将其字符串表示形式发送为响应正文。

app.route()

您可以使用来为路由路径创建可链接的路由处理程序app.route()。由于路径是在单个位置指定的,因此创建模块化路由非常有帮助,减少冗余和错别字也很有帮助。有关路由的更多信息,请参见:Router()文档

这是使用定义的链式路由处理程序的示例app.route()

1
2
3
4
5
6
7
8
9
10
11
app
.route('/book')
.get(function (req, res) {
res.send('Get a random book')
})
.post(function (req, res) {
res.send('Add a book')
})
.put(function (req, res) {
res.send('Update the book')
})

快速路由器

使用express.Router该类创建模块化的,可安装的路由处理程序。一个Router实例是一个完整的中间件和路由系统; 因此,它通常被称为“迷你应用程序”。

以下示例将路由器创建为模块,在其中加载中间件功能,定义一些路由,并将路由器模块安装在主应用程序的路径上。

birds.js在 app 目录中创建一个名为以下内容的路由器文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var express = require('express')
var router = express.Router()

// middleware that is specific to this router
router.use(function timeLog(req, res, next) {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', function (req, res) {
res.send('Birds home page')
})
// define the about route
router.get('/about', function (req, res) {
res.send('About birds')
})

module.exports = router

然后,在应用程序中加载路由器模块:

1
2
3
var birds = require('./birds')
// ...
app.use('/birds', birds)

该应用程序现在将能够处理对/birds和的请求/birds/about,以及调用timeLog特定于该路线的中间件功能。