Open yaofly2012 opened 5 years ago
app.METHOD(PATH, HANDLER)
Use the express.Router class to create modular, mountable route handlers 使用Router方法创建模块化的,可挂载的路由处理函数。可以实现
子路由
匹配。
多handlers的绑定方式注意:
app.get('/user/:id', function (req, res, next) {
next()
}, function(req, res, next) {
res.send('regular')
})
// 而不是多次调用app.METHOD(即使path相同也是多个路由,不是多个Handler)
app.get('/user/:id', function (req, res, next) {
if(req.params.id === '0') next('route');
else next()
})
app.get('/user/:id', function (req, res, next) {
if(req.params.id === '0') next('route');
else next()
})
app.use('/', (req, res) => {
res.send('Welcome My Site')
});
app.use('/', Router对象);
## API
1. express.Router
2. app.route
3. app.METHOD(PATH, HANDLER)
是中间件语法糖?
# 参考
1. [Differences between express.Router and app.get?](https://stackoverflow.com/questions/28305120/differences-between-express-router-and-app-get)
就是个函数function(req, res, next) {}
app.use方法,
next('route') to pass control to the next route. next('router') to pass control back out of the router instance.
所以route
和router
的区别?
route
是路由,而router
是express的概念是Router的实例对象。Router对象也可以有多个route
(即子路由),路由可以依据path的分层结构而分层表示。
由中间件函数构成了一个队列,中间件就像流水线各个节点,req-res进过这些中间件被处理。
app.use(function myLogger(req, res, next) {
console.log('myLogger1 before')
next();
console.log('myLogger1 after')
})
app.use(function myLogger(req, res, next) {
console.log('myLogger2 before')
next();
console.log('myLogger2 after')
})
app.get('/', (req, res) => {
console.log('handle')
res.send('Hello World!')
})
中间件的功能无限大。
原则上express能捕获req-res cycle中的异常,异步代码会脱离express控制,需要显示处理。
调用next函数传参不同,express行为不同,怎么判断参数的
next(), next('route'), next('router'), next(error)
app.use('/hello', function fn1(req, res, next) {
res.send('hello World')
})
app.get('/hello', function fn2(req, res, next) {
res.send('hello World')
})
app.get('/book', (req, res) => { res.send('book') })
匹配:
request.params
对象获取路径参数create chainable route handlers Because the path is specified at a single location, creating modular routes is helpful, as is reducing redundancy and typos.
app.route(path)
如果对单一路径绑定指定对多种
method
对处理函数可以使用route
方法。
all
方法会绑定所有Use the express.Router class to create modular, mountable route handlers.
把路由处理函数通过模块文件的方式组织,让项目结构更清晰,方便维护。
next
函数,进入下一个中间件函数,否则请求会一直被挂起等待处理。req
和res
对象都是公用对对象,可以在上面添加自定义属性,把自己处理对结果添加到req或者res对象上。好多中间件处理函数都是这样做对。express app -> Router -> Route -> Request Handlers 都是中间件。
next()
流转到下一个处理函数;next(route)
跳过当前route到剩余处理函数,流转到下一个route;next(router)
跳过当前router剩余到route,流转到下一个router;next(error)
?Express捕获,处理异常机制。
路由处理函数和中间件函数内部可以执行任意同步或者异步代码,如果代码执行发生异常并且想让Express处理这些异常,则必须告诉Express发生异常了(即让Express捕获到异常)。
next
函数,否则Express无法处理,需要开发自己处理,如果开发也没处理则一直往上抛了,应用就挂了。
next函数如何区分各种不同实参调用逻辑的?If you had done this processing inside the readFile callback then the application might exit and the Express error handlers would not run.
啥意思?
So when you add a custom error handler, you must delegate to the default Express error handler, when the headers have already been sent to the client.
这是因为默认异常处理中间件会
the Express default error handler closes the connection and fails the request.
????
异常处理也是个中间件。那是如何被执行到呢?
If you pass anything to the next() function (except the string 'route'), Express regards the current request as being an error and will skip any remaining non-error handling routing and middleware functions.
What is Express ?
一、概述
二、构成
express = 路由匹配(routing) + 中间件(middleware) ?
核心概念: