swagger的响应模式代码与node.js'的匹配程度如何?响应状态代码?

时间:2017-11-06 10:07:25

标签: node.js swagger

我是一个狂热的人。我想使用Online Swagger Editor来测试一些例子。但我作为图像遇到了问题[ enter image description here]。

以下是我在Swagger编辑器中的设计:

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'。

有人能处理吗?非常感谢您回答我的问题!

1 个答案:

答案 0 :(得分:0)

nodejs中的

使用函数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"});
       }
    });
});

YAML格式的招摇示例:

swagger: '2.0'
info:
  title: EXAMPLE
  description: Documentation ......
  version: 1.0.0
  contact:
    email: richych92@gmail.com
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