我已经创建了一个用于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\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)
..
const express = require('express');
const Joi = require('joi');
const router = express.Router();
const HelperJoi = require('../helpers/joivalidation');
router.get('/GetRetailerDetails',VerifyToken, async (req,res) => {
//Start Validation Code
const schema = {
RetailerId: Joi.number().required(),
};
HelperJoi.validate(req,res,schema);
//End of Validation code
});
joivalidation.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
答案 0 :(得分:2)
仅仅因为验证器函数在出现错误的情况下返回响应并不意味着其余代码没有在运行。
因此,您需要与中间件通信,以确认验证失败,并且中间件不应继续。
例如:
// 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响应本身。那将使验证器功能解耦。
答案 1 :(得分:0)
HelperJoi.validate是其中的一个函数,您可以发送响应,然后返回到调用函数,该函数是路由处理程序,并尝试尝试终止requset并将其设置为已发送的标头。可以将验证用作中间件,也可以从HelperJoi.validate返回验证错误,然后从路由处理程序进行响应。
答案 2 :(得分:0)
看起来您的实现是错误的。
示例:如何实现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
const Joi = require('joi');
const joiHelper = {};
joiHelper.JoiValidation(object, schema, options = {}){
return Joi.validate(object, schema, options);
};
joiHelper.parseError(errorObject) {
let errorDetail = [];
for (var key in errorObject) {
errorDetail.push(errorObject[key].message);
}
return errorDetail.join(', ').replace(new RegExp('"','g'),'');
}
joiHelper.getRetailSchema () {
const schemaObj = {
RetailerId: Joi.number().required()
};
return Joi.object().keys(schemaObj);
};
module.exports = joiHelper;