basic route
const express=require(‘express’);
var app=express();
app.methds(“path”,(req,res)=>{});
每个路由可以包含next(),相当于一个callback的参数,作用是切换当前控制到下一个callback
methods的种类:
- app.get("/",(req,res)=>{})
- app.post("/",(req,res)=>{})
- app.put("/",(req,res)=>{})
- app.delete("/",(req,res)=>{});
- 一个特殊的路由method——app.use(),是用来导入中间件的function作为所有的http方式的path,支持上述四种请求方式以及其他任何请求方式
app.all('/secret', function (req, res, next) {
console.log('Accessing the secret section ...')
next() // pass control to the next handler
})
路由中的路径格式
支持string、正则表达式、string patterns
?、+、*、()可以看做是正则表达式的子集,这些符号最后将url字符串逐路径解析为-或者是dot(.),如果使用
符
号
,
则
最
好
使
用
(
[
]
)
将
他
包
围
,
e
g
.
p
a
t
h
为
/
d
a
t
a
/
符号,则最好使用([])将他包围,eg.path为/data/
符号,则最好使用([])将他包围,eg.path为/data/book应该写为/data/([$])book
app.get('/ab(cd)?e', function (req, res) {
res.send('ab(cd)?e');//This route path will match /abe and /abcde.
})
app.get(/.*fly$/, function (req, res) {
res.send('/.*fly$/');//This route path will match butterfly and dragonfly, but not butterflyman, dragonflyman, and so on.
})
路由params
app.get(’/users/:userId/books/:bookId’, function (req, res) {
res.send(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” }
Route path: /user/:userId(\d+)
Request URL: http://localhost:3000/user/42
req.params: {“userId”: “42”}
路由处理程序
1.可以像中间件一样使用多个callback处理一个请求。
app.get("/",(req,res)=>{
console.log("即将进入nex");
next()},
(req,res)=>{
res.send('Hello from B!')
});
-
callback可以是一个function数组
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])
-
可以是一个独立的function和function数组组成
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!')
})
Response methods
下表中的methods可以给客户端发送一个res回应
app.route()
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')
})
expreee.Router
通过express的Router()类创建一个模块或者可挂载的route处理程序。一个Router实例可以被看作是一个完整的中间件或者路由系统,
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
然后在其他文件中可以引入路由模块:
var birds = require(’./birds’)
// …
app.use(’/birds’, birds);