2017-02-09 93 views
0

我正在使用POST動詞創建一個API端點,並且需要一個文件。我宣佈我的終點如下:用swaggerize-hapi和node.js上傳文件?

consumes: 
    - application/json 
    - multipart/form-data 
    # format of the responses to the client (Accepts) 
    produces: 
    - application/json 
... 
    /importfile: 
     x-swagger-router-controller: import_file 
     post: 
      description: Sends the uploaded file to deliveries microservice to be processed. 
      consumes: 
      - multipart/form-data 
      produces: 
      - application/json 
      operationId: importcsv 
      parameters: 
      - name: file 
       in: formData 
       description: file to upload 
       required: true 
       type: file 

,並在import_file.js

const util = require('util'); 

function importcsv(req, res) { 
    console.log(req); 
    const message = util.format('hi there!'); 
    // this sends back a JSON response which is a single string 
    return res.json(message); 

} 

module.exports = { 
    importcsv, 
}; 

夫婦的問題,我有以下幾點:

  1. 日誌中的路由器控制器從不打印。它讓我覺得這個函數沒有被調用。哪裏不對?
  2. 即使成功響應返回我看到這一點:

    HTTP/1.1

    內容類型:text/html的;字符集= UTF-8 緩存控制:無緩存

    多部分:邊界沒有找到

這是爲什麼?

我是新來的Swagger。提前致謝。

+0

res.json看起來不正確,你怎麼聲明配置關聯的處理器的路線? –

回答

-1

此帖子here讓我覺得邊界問題已修復。那麼它是固定並於2016年12月29日合併到主分支,但最新版本的Swagger(see)是在2016年12月27日製作的。

所以,文件上傳目前無法通過Swagger (v1.5.12)。這回答了我的兩個問題。

我寫了下面的單元測試來測試我的控制器,它的工作原理:

const describe = require('ava-spec').describe; 
const request = require('supertest-as-promised'); 
const server = require('../../../../app'); 

describe('controllers',() => { 
    describe('import_file',() => { 
     describe('POST /importfile', (it) => { 
      it('should accept a file parameter', async (t) => { 
       const res = await request(server) 
        .post('/importfile') 
        .attach('file', 'test/data/A.csv') 
        .expect(200); 
       t.is(res.body, 'file uploaded'); 
      }); 
     }); 
    }); 
});