嵌套路由器不会返回下一个回调值为'route'
Nested router doesn't return next callback with value of 'route'
我正在为中间件构建一个处理程序,我发现当您返回route
字符串作为next
回调的参数时,它的值是null
。
示例如下:
var express = require('express')
var app = express()
app.use('/', function (req, res, next) {
var router = express.Router()
router.use(function (req, res, next) {
return next('route')
})
return router(req, res, function (nextValue) {
console.log('// value of next')
console.log(nextValue)
return next(nextValue)
})
})
app.use('/', function (req, res, next) {
return res.send('hi')
})
module.exports = app
这意味着你不能像这样传递下一个处理程序:
app.use('/', function (req, res, next) {
var router = express.Router()
router.use(function (req, res, next) {
return next('route')
})
return router(req, res, next)
})
我知道这看起来很多余,因为你可以这样做:
app.use('/', function (req, res, next) {
return next('route')
})
然而,我正在构建一个需要以这种方式使用嵌套中间件的库。似乎我唯一的选择是使用不同的字符串,因为如果我这样做:
router.use(function (req, res, next) {
return next('anystring')
})
next
回调为nextValue
提供anystring
。
为什么字符串route
不通过嵌套中间件传播?
对于express来说,不返回route
似乎是有意义的,因为在该点路由,该路由已经完成。
首先.use
不支持next('route')
。所以我用.all
代替。即使这样也不会返回字符串"route"。所以我需要在路由器中注入一些中间件。如果nextRoute
的值没有更新,那么next('route')
在中间件堆栈期间的某个时候被调用,我可以将其向上传播到父中间件。
我发现我必须在
的末尾注入一个中间件。app.use(function (req, res, next) {
var router = express.Router()
var nextRoute = true
router.all('/', [
function (req, res, next) {
return next('route')
},
function (req, res, next) {
nextRoute = false
return res.send('hi')
},
])
return router(req, res, function (nextValue) {
if (nextRoute && !nextValue) return next('route')
return next(nextValue)
})
})
app.use('/', function (req, res, next) {
return res.send('hi')
})
这允许我的middleware-nest
模块工作:
var _ = require('lodash')
var express = require('express')
/** Allow for middleware to run from within middleware. */
function main (req, res, next, Router, middleware) {
var args = _.values(arguments)
middleware = _.flatten([_.slice(args, 4)])
Router = (Router) ? Router : express.Router
var router = Router()
var nextRoute = true
middleware.push(function (req, res, next) {
nextRoute = false
return next()
})
router.all('*', middleware)
return router(req, res, function (nextValue) {
if (nextRoute && !nextValue) return next('route')
return next(nextValue)
})
}
main.applyRouter = function (Router) {
Router = (Router) ? Router : express.Router
return function () {
var args = _.values(arguments)
args.splice(3, 0, Router)
return main.apply(null, args)
}
}
module.exports = main
相关文章:
- AngularJS:我可以跳过函数参数回调吗
- 要求未定义JS回调参数
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 自引用回调
- 测试Angular Service解决错误回调中的promise
- 如何将一个JavaScript函数回调为多个函数
- JavaScript回调函数
- 用于回调的javascript参数
- 将json回调数据转换为日期
- 承诺在非节点式回调上使用Bluebird
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 如何使用jqueryAJAX从页面中回调多个变量
- jquery Onclick函数带有导致双击的回调排序函数
- Javascript点击事件回调不起作用
- 一旦加载并渲染了角度引导typeahead,就使用回调
- google在类内映射javascript directionsService.route请求:将类实例传递给回调函数
- MEANJS TypeError:Route.all()需要回调函数,但得到了一个[object Undefined]
- 错误:Route.put()需要回调函数,但得到了一个[object Undefined]