2017-11-06 7 views
0

私はすごく斬新です。私はいくつかの例をテストするためにOnline Swagger Editorを使用したいと思います。しかし、私はイメージ[ enter image description here]として問題を抱えていました。swaggerのレスポンススキーマのコードがnode.jsのレスポンスステータスコードとどのように一致しますか?

次が闊歩エディタで私のデザインで次のように

swagger: '2.0' 
info: 
    version: 1.0.0 
    title: API example 
    description: An API example to pratice. 
schemes: 
     - https 
     - http 
    host: '127.0.0.1:3000' 
    basePath: /users 
    paths: 
     /connect: 
     get: 
      summary: get some infos! 
      description: get some info! 
      responses: 
      '200': 
       description: haha success! 
      '404': 
       description: not found! 
      default: 
       description: error!!!! 

そして、私のNode.jsのコードは以下のとおりです。

var express = require('express'); 
var router = express.Router(); 

/* GET users listing. */ 
router.get('/connect', function(req, res, next) { 
    console.log("111"); 
    res.send('200'); 
}); 

module.exports = router; 

私はNode.jsのが返す方法がわかりません'200'や '404'のようなコードです。

誰かがそれを処理できますか?私の質問に答えてくれてありがとう! YAMLフォーマットでnodejsで

答えて

0

(関数res.statusを使用)

var express = require('express'); 
var router = express.Router(); 

/* GET users listing. */ 
router.get('/connect', function(req, res, next) { 
    modelUser.findAll().then(function (data) { 
     if (data.length > 0) { 
      res.status(200).send({success: true, data: data}); 
     } else { 
      res.status(404).send({success: false, message: "some message"}); 
     } 
    }); 
}); 

闊歩例:

swagger: '2.0' 
info: 
    title: EXAMPLE 
    description: Documentation ...... 
    version: 1.0.0 
    contact: 
    email: [email protected] 
schemes: 
    - http 
host: '192.168.1.55:8280' 
basePath: /fojalAcademia/v1 
tags: 
    - name: Conect 
    description: resources user...... 
paths: 
    /conect: 
    get: 
     tags: 
     - Conect 
     summary: users list 
     description: retun users array 
     produces: 
     - application/json 
     responses: 
     '200': 
      description: data succes 
      schema: 
      $ref: '#/definitions/resUsersSuccess' 
     '404': 
      description: data error 
      schema: 
      $ref: '#/definitions/resUserError' 

definitions: 
    resUsersSuccess: 
    type: object 
    properties: 
     success: 
     type: boolean 
     description: true or false..in this case true 
     data: 
     type: array 
     items: 
      $ref: '#/definitions/dataUser' 
    dataUser: 
    type: object 
    properties: 
     id: 
     type: integer 
     format: int64 
     description: user id 
     email: 
     type: string 
     description: user email 
     edad: 
     type: integer 
     format: int64 
     description: user´s age 

    resUserError: 
    type: object 
    properties: 
     success: 
     type: boolean 
     description: true or false..in this case false 
     message: 
     type: string 
     description: error message 
+0

はい、それはそれです!どうもありがとう! – qitian

関連する問題