Node.js 错误:Can';t在发送到中间件进行joi验证后设置标头
我为joi验证错误处理创建了一个中间件,但它返回一些警告,如“错误:发送后无法设置头” 错误:Node.js 错误:Can';t在发送到中间件进行joi验证后设置标头,node.js,express,joi,Node.js,Express,Joi,我为joi验证错误处理创建了一个中间件,但它返回一些警告,如“错误:发送后无法设置头” 错误: Error: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:491:11) at ServerResponse.setHeader (_http_outgoing.js:498:3) at ServerResponse.header (D:\nodejs\bigfish\n
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (D:\nodejs\bigfish\node_modules\express\lib\response.js:767:10)
at ServerResponse.send (D:\nodejs\bigfish\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (D:\nodejs\bigfish\node_modules\express\lib\response.js:267:15)
at Object.JSONResponse [as JR] (D:\nodejs\bigfish\helpers\JsonResponse.js:3:29)
at module.exports (D:\nodejs\bigfish\middlewares\error.js:8:14)
at newFn (D:\nodejs\bigfish\node_modules\express-async-errors\index.js:12:20)
at Layer.handle_error (D:\nodejs\bigfish\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (D:\nodejs\bigfish\node_modules\express\lib\router\index.js:315:13)
helpers文件夹下的joivalization.js
const Joi = require('joi');
//This is Helper Function Module for Joi Validation
function JoiValidation(req,res,schema){
const result = Joi.validate(req.query,schema, { abortEarly: false });
if(result.error){
//422 Validation Error
var objError = [];
Object.keys(result.error.details).forEach(function(key) {
objError.push(result.error.details[key]['message']);
});
return res.status(422).json({
'statuscode': 422,
'message': 'Validation Error',
'responsedata': objError
});
}
}
module.exports.validate = JoiValidation
验证程序函数在出现错误时返回响应并不意味着其余代码没有运行 因此,您需要向中间件反馈验证失败的消息,并且中间件不应继续 例如:
// middleware
if (! HelperJoi.validate(req,res,schema)) {
// Validation failed, so we're done
return;
}
// validator function
if (result.error) {
...
res.status(422).json({
'statuscode': 422,
'message': 'Validation Error',
'responsedata': objError
});
return false;
}
return true;
另一种解决方案是,如果验证为正,验证器函数将返回“null”,如果验证失败,则返回一组错误,中间件将返回422响应本身。这将解耦验证器函数。仅仅因为验证器函数在出现错误时返回响应并不意味着其余代码没有运行 因此,您需要向中间件反馈验证失败的消息,并且中间件不应继续 例如:
// middleware
if (! HelperJoi.validate(req,res,schema)) {
// Validation failed, so we're done
return;
}
// validator function
if (result.error) {
...
res.status(422).json({
'statuscode': 422,
'message': 'Validation Error',
'responsedata': objError
});
return false;
}
return true;
另一种解决方案是,如果验证为正,验证器函数将返回“null”,如果验证失败,则返回一组错误,中间件将返回422响应本身。这将解耦验证器函数。HelperJoi.validate是其中的一个函数。您发送一个响应,然后返回到调用函数,该函数是路由处理程序,并尝试终止请求并设置为已发送的标头。将验证用作中间件,或从HelperJoi.validate返回验证错误,并从路由处理程序响应。HelperJoi.validate是其中的一个函数。您发送响应,然后返回到调用函数,该函数是路由处理程序,并尝试终止requset并设置为已存在的标头发送。将验证用作中间件或从HelperJoi返回验证错误。验证并从路由处理程序响应。看起来您的实现是错误的 示例:如何实现
JOI
验证
const express = require('express');
const router = express.Router();
const joiHelper = require('../helpers/joiHelper');
router.get('/GetRetailerDetails',VerifyToken, async (req,res) => {
let validationResult = joiHelper.JoiValidation(req.query, joiHelper.getRetailSchema(), {allowUnknown: false});
if (validationResult.error) {
return res.status(422).json({
'statuscode': 422,
'message': 'Validation Error',
'responsedata': joiHelper.parseError(validationResult.error.details)
});
}
//Proceeds your get request
});
joiHelper.js
看起来您的实现是错误的 示例:如何实现
JOI
验证
const express = require('express');
const router = express.Router();
const joiHelper = require('../helpers/joiHelper');
router.get('/GetRetailerDetails',VerifyToken, async (req,res) => {
let validationResult = joiHelper.JoiValidation(req.query, joiHelper.getRetailSchema(), {allowUnknown: false});
if (validationResult.error) {
return res.status(422).json({
'statuscode': 422,
'message': 'Validation Error',
'responsedata': joiHelper.parseError(validationResult.error.details)
});
}
//Proceeds your get request
});
joiHelper.js