使用swagger-express-mw上传文件

时间:2019-02-07 20:03:35

标签: express file-upload swagger

我有一个swagger 2.0 / express后端,我正在尝试使用swagger-express-mw npm软件包设置图像上传。 如果我只想将图像保存在服务器中,可以避免使用aws还是swagger-express-mw必须与aws一起使用?

我在下面的网上找到了这段代码来设置中间件,但是我不知道在控制器中放什么。有人知道如何在没有aws的情况下进行设置吗?

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');
        }
    });

});

这是我的招摇:

  /images:
    x-swagger-router-controller: images
    post:
      summary: Uploads a file.
      operationId: upload
      consumes:
        - multipart/form-data
      parameters:
        - in: formData
          name: upfile
          type: file
          description: The file to upload.
      responses:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/SuccessResponse"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

0 个答案:

没有答案