node.js - 特定中间件上的 bodyParser.json
问题描述
基本上,我需要对请求进行 bodyParse 以便能够使用 express-recaptcha,因此需要在该中间件中对其进行 bodyParse,但仅限于整个请求而不是整个请求。
这是我想要使用 bodyParser 的中间件(recaptcha.verify 期望它是 json 解析器):
const recaptchaMiddleware = function(req, res, next) {
return recaptcha.verify(req, function(error, data) {
logger.log({
level: 'debug',
message: 'Verifying recaptcha token',
})
if (error) {
logger.log({
level: 'error',
message: `Error verifying recaptcha token for request`,
body: error,
})
return res.status(403).send()
}
next()
})
}
这是我的路线:
.use(
'/api/v1/mandrill',
recaptchaMiddleware,
function (req, res, next) {
logger.log({
level: 'debug',
message: 'Debugme',
body: JSON.stringify(req.body),
})
next()
},
createProxy(EMAIL_API, { onProxyRes: metricsMiddleware('emailservice', []) })
)
我可以做以下悬停我代理的服务不喜欢那个....
const jsonParser = bodyParser.json({ limit: '10mb' })
.use(
'/api/v1/mandrill',
jsonParser,
recaptchaMiddleware,
function (req, res, next) {
logger.log({
level: 'debug',
message: 'Debugme',
body: JSON.stringify(req.body),
})
next()
},
createProxy(EMAIL_API, { onProxyRes: metricsMiddleware('emailservice', []) })
)
摘要:我需要在 recaptcha.verify(req, function(error, data) 而不是整个路由上使用 bodyParser.json()
解决方案
推荐阅读
- c# - 如何使用 LINQ 使用双组元素?
- c++ - 交友类,而交友类里面有交友类的对象
- ios - Xcode 12 推送通知切换为空
- ios - Swift - #selector 方法没有被调用
- sql-server - 不同 Top 计数的 SQL Server 执行计划
- vue.js - Vuex状态数组在变异时转换代理对象
- visual-studio - 部署 SSIS 包时 Visual Studio 2012 崩溃
- r - 从 Reshape 转换为 Reshape 2 时融化不起作用
- c# - 当我将我的程序放入不同的方法中,并将它们组合成一个循环时,它们不能正确地相互读取。有谁知道为什么?
- flutter - Flutter,如何从子级更改当前标签栏视图或页面或索引