gpt4 book ai didi

javascript - Express 4 奇怪的动态中间件行为

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:34 25 4
gpt4 key购买 nike

我试图在快速 4 路由中调用动态创建的中间件,但它永远不会被调用并创建超时。

'use strict';
var bodyParser = require( 'body-parser' );
var logger = require( './logger' );
var db = require( './mongoose' );
var validations = require( './validations' );

module.exports = function (app, config, done) {

logger.debug( "Overriding 'Express' logger" );
app.set( 'trust proxy', true );
app.set( 'x-powered-by', false );

app.use( require( 'morgan' )( 'dev', {"stream": logger.stream} ) );
app.use( bodyParser.urlencoded( {extended: true} ) );

app.post( '/api/login', [
function (req, res, next) {
console.log( 'check post' );
},
validations.login.post.validate,
function (req, res, next) {
console.log( 'Succeful post' );
console.dir( req.user );
}] );
};

这里 validations.login.postValidationMiddleware 类的一个对象,validate 方法是一个中间件,但它永远不会被调用。

var _ = require( 'lodash' );

var defaultOptions = {
convert : true,
abortEarly : false,
stripUnknown: true
};

function ValidationMiddleware(schema, joi) {
this.arr = {};
this.joi = joi;
if (!schema.options) schema.options = {};
this.options = _.clone( _.defaults( schema.options, defaultOptions ) );
if (schema.params && Object.getOwnPropertyNames( schema.params ).length > 0) this.arr['params'] = joi.compile( schema.params );
if (schema.query && Object.getOwnPropertyNames( schema.query ).length > 0) this.arr['query'] = joi.compile( schema.query );
if (schema.body && Object.getOwnPropertyNames( schema.body ).length > 0) this.arr['body'] = joi.compile( schema.body );
//this.validate = this.validate.bind( this );
}

ValidationMiddleware.prototype.validate = function (req, res, next) {
console.err("validate called")
var parent = this;
var flag = true;
_.forEach( _.keys( this.arr ), function (key) {

if (key == 'options') return true;
var result = parent.joi.validate( req[key], parent.arr[key], parent.options );
if (result.error) {
res.status( 400 ).send( {
message: 'incorrect values in ' + key,
data : result.error
} );
flag = false;
return false;
}
//TODO: Assign values to
req[key] = result.value;
} );
if (flag) next();

};

function ValidationMiddlewareBuilder(joi) {
this.joi = joi;
this.build = function (schema) {
return new ValidationMiddleware( schema, joi );
}
}

module.exports = ValidationMiddlewareBuilder;

我尝试过调试并记录到控制台,但在打印检查后什么也没有。希望得到任何提示来推进这一工作。我一直在另一个 Express 3 项目中使用它,它仍然有效,但在这种情况下有些东西让它失效。当验证文件加载架构并创建动态 ValidationMiddleware 对象时,看起来没问题,但当路由被调用时,一切似乎都丢失了。

构建方法在另一个名为validations.js的文件中调用

    fs
.readdirSync( 'routes/validations' )
.filter( function (file) {
return (file.indexOf( '.' ) !== 0);
} )
.forEach( function (file) {
console.log( 'Loading validation file ' + file );
var models = new require( path.join( config.rootPath + '/routes/validations', file ) )( joi );
var builder = new ValidationMiddlewareBuilder( joi );
validations[file.split( '.' )[0]] = {};
_.keys( models ).forEach( function (key) {
validations[file.split( '.' )[0]][key] = builder.build( models[key] );
} )
} );

module.exports = validations;

最佳答案

问题是您没有在第一个路由处理程序中调用 next() ,因此请求不会流向下一个处理程序(您的请求验证中间件)。由于您没有在第一个路由处理程序中返回响应,因此会出现超时。

试试这个:

app.post( '/api/login', [
function (req, res, next) {
console.log( 'check post' );
next(); // <---- add this
},
validations.login.post.validate,
function (req, res, next) {
console.log( 'Succeful post' );
console.dir( req.user );
}] );

另外,FWIW,您应该能够在不使用数组的情况下将多个中间件传递给路由函数。示例:

app.post( '/api/login',
function (req, res, next) {
console.log( 'check post' );
},
validations.login.post.validate,
function (req, res, next) {
console.log( 'Succeful post' );
console.dir( req.user );
} );

关于javascript - Express 4 奇怪的动态中间件行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28326720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com