How to upload a file using swagger-express-mw and express?

I want to download multi-page data forms using express framework. I am using swagger-node with express for my APIs. Now I wrote the following in the YAML swagger file to download the file:

  /picture/students:
    # binds a127 app logic to a route
    x-swagger-router-controller: bus_api
    post:
      description: Upload a picture
      # used as the method name of the controller
      operationId: uploadStudentPic
      consumes:
        - multipart/form-data
      parameters:
       - in: formData
         name: imageFile
         type: file
         description: The file to upload.
         required: true
      responses:
        "200":
          description: OK
          schema:
            # a pointer to a definition
            $ref: "#/definitions/SuccessResponseStr"

But now I do not know how to upload an image using it. Is there a built-in tool for uploading images to swagger?

+4
source share
1 answer

, . express-fileupload npm, . , yaml .

 //import the module
 const fileUpload = require('express-fileupload');

 // Add this in express setup 
 app.use(fileUpload());

 // Your controller function, where you will get the file and store it as needed
 function uploadStudentPic(req: any, res: any, next: any) {

   var startup_image = req.files.imageFile;
   var fileName = startup_image.name;

   startup_image.mv(__dirname + '/images/' + fileName, function (err: any) {
     if (err) {
       res.status(500).send(err);
     }
     res.json({
       "message": "File Uploaded"
     });
   });
 }

, Swagger , , Swagger .

, API , ...

  parameters:
   - in: formData
     name: imageFile
     type: file
     description: The file to upload.
     required: true

... , API POST imageFile API. - - swagger, API 400 Bad Request .

Swagger Swagger Express Middleware, , ( : P)

0

Source: https://habr.com/ru/post/1682604/


All Articles