如何在swagger express中附加中间件,例如express-validator [英] how to attach middleware in swagger express like express-validator

查看:162
本文介绍了如何在swagger express中附加中间件,例如express-validator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编辑我的express-swagger项目,并按照本教程将输入验证添加到快速应用程序中

I am trying to edit my express-swagger project and following this tutorial to add input validation into an express app

我使用 swagger-express-mw 包使用,但不清楚如我上面提到的教程中所述,我可以在哪里添加中间件.具体来说,我无法拦截我的请求,但出现typeError:

I used swagger-express-mw package to generate a boilerplate using swagger project create app but its not clear where I can add my middlewares as explained in the tutorial I mentioned above. Specifically i cant intercept my request, I get a typeError:

    TypeError: req.checkBody is not a function
     at saveNote 

这是我的输入文件.除了bodyParser和Validator函数之外的所有内容都来自样板,所以我不理解

Here is my entry file. Everything apart from the bodyParser and validator function is coming from the boilerplate so I dont understand

    SwaggerExpress.create(config, function(err, swaggerExpress) {
  if (err) { throw err }

  // install middleware
  swaggerExpress.register(app)
  // middleware
  app.use(bodyParser.urlencoded({ extended: false })); // my code
  app.use(validator()) // my d

  const port = process.env.PORT || 10010
  app.listen(port)


  // if (swaggerExpress.runner.swagger.paths['/notes']) {
  //   console.log('try this:\nlocalhost:10010/notes to get all notes')
  // }
})

推荐答案

我知道这是一个古老的问题,但这可能会对整个过程有所帮助.这是简单的NodeJS代码,显示了如何将swagger-toolsswagger-node-runner npm软件包.

I know this is an old question, but this might help someone along the way.Here is the simple NodeJS code showing how to integrate swagger-tools with swagger-express-mw or swagger-node-runner npm packages.

var SwaggerExpress = require('swagger-express-mw');
var swaggerTools = require('swagger-tools');

var app = require('express')();
module.exports = app; // for testing

var config = {
    appRoot: __dirname // required config
};

SwaggerExpress.create(config, function(err, swaggerExpress) {
    if (err) {
        throw err;
    }

    // install middleware
    swaggerExpress.register(app);

    //swaggerobject is created by the swagger-express-mw module which is stored in runner object.
    var swaggerObjectLoaded = swaggerExpress.runner.swagger;

    //pass the runner object to initializeMiddleware function 
    swaggerTools.initializeMiddleware(swaggerObjectLoaded, function(middleware) {


        app.use(middleware.swaggerMetadata());

        // Validate Swagger requests
        app.use(middleware.swaggerValidator());

        // Route validated requests to appropriate controller
        app.use(middleware.swaggerRouter(options));

        // Serve the Swagger documents and Swagger UI
        app.use(middleware.swaggerUi());

        var port = process.env.PORT || 10010;
        app.listen(port);

        if (swaggerExpress.runner.swagger.paths['/hello']) {
            console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
        }
    });

});

这篇关于如何在swagger express中附加中间件,例如express-validator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆