没有内容的Swagger状态代码200

时间:2016-09-28 15:48:29

标签: javascript node.js express swagger

当我在swagger中调用我的示例API时,我得到状态200而没有内容。 我使用我的本地Swagger Ui /编辑器和浏览器(Chrome)调用了API。

我正在使用swagger + express并使用swagger -g和 招摇快车中间件

我编辑了我的befundGet函数来响应一个JSON字符串和一个似乎甚至没有运行的控制台日志。

为什么当我的api呼叫甚至没有调用swagger中定义的函数时,它甚至可以运行200 OK?

这是我的节点服务器的Index.js:

var express = require("express");
var app = express();
var swaggerMW = require('swagger-express-middleware');
var MongoClient = require('mongodb').MongoClient;

// Connection URL
var url = 'mongodb://localhost:27017/test';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
    if(err){
        console.log("Connection couldn't be established");
        return false;
    }

    console.log("Connected successfully to server");
    //do stuff
    /*insertDocuments(db,function (result) {
        console.log(result);
    });*/
    db.close();
});

swaggerMW('api/swagger/swagger.yaml', app, function(err, swaggerMW) {
    // Add all the Swagger Express Middleware, or just the ones you need.
    // NOTE: Some of these accept optional options (omitted here for brevity)
    app.use(
        swaggerMW.metadata(),
        swaggerMW.CORS(),
        swaggerMW.files(),
        swaggerMW.parseRequest(),
        swaggerMW.validateRequest(),
        swaggerMW.mock()
    );
});
/*
var insertDocuments = function(db, callback) {
    // Get the documents collection
    var collection = db.collection('documents');
    // Insert some documents
    collection.insertMany([
        {a : 1}, {a : 2}, {a : 3}
    ], function(err, result) {
        assert.equal(err, null);
        assert.equal(3, result.result.n);
        assert.equal(3, result.ops.length);
        console.log("Inserted 3 documents into the collection");
        callback(result);
    });
}*/

function validateUser(req){
   return true
}

app.get("/",function (req,res) {
    validateUser(req);
    console.log('got req');
    res.sendFile(__dirname + "/public/index.html");
});
app.get("/swag",function (req,res) {
    validateUser(req);
    console.log('got req');
    res.sendFile(__dirname + "/public/swagger-ui/dist/index.html");
});
app.get("/api/swagger",function (req,res) {
    validateUser(req);
    res.sendFile(__dirname + "/api/swagger/swagger.yaml");
});
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/public/swagger-ui/dist'));

app.listen(4000);

我几乎使用了readme中的标准swaggerMW设置,只是我从中间件中删除了app.listen()。

这是我使用swagger编辑器构建的swagger.yaml:

swagger: "2.0"
info:
  version: "0.0.1"
  title: Toothyfy API
  description: The Toothyfy API for writing/getting docs from the MongoDB. Toothyfy has no production enviroment and is only available in a unstable development enviroment with constant changes.
# during dev, should point to your local machine
host: localhost:4000
# basePath prefixes all resource paths
basePath: /api
#tags
tags:
- name: patients
  description: patients API calls
- name: befunde
  description: befunde API calls
- name: user
  description: user API calls
- name: pflegeheime
  description: pflegeheime API calls
#
schemes:
  # tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /patients:
    # binds swagger app logic to a route
    x-swagger-router-controller: patients
    get:
      tags:
      - patients
      description: Returns Patients to caller
      # used as the method name of the controller
      operationId: patientsGet
      parameters:
        - name: pflegeheim
          in: query
          description: limit the patients to a certain pflegeheim
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            type: array
            items: {}
    put:
      tags:
      - patients
      description: Puts patients to DB 
      # used as the method name of the controller
      operationId: patientsSet
      responses:
        "200":
          description: Success
          schema:
            type: string
            # a pointer to a definition              
  /pflegeheime:
    # binds swagger app logic to a route
    x-swagger-router-controller: pflegeheim
    get:
      tags:
      - pflegeheime
      description: Returns Pflegeheime to caller
      # used as the method name of the controller
      operationId: pflegeheimGet
      parameters:
        - name: pflegeheim
          in: query
          description: limit to a certain pflegeheim
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            type: array
            items: {}
    put:
      tags:
      - pflegeheime
      description: Puts patients to DB 
      # used as the method name of the controller
      operationId: pflegeheimSet
      responses:
        "200":
          description: Success
          schema:
            type: string
            # a pointer to a definition            
  /user:
    # binds swagger app logic to a route
    x-swagger-router-controller: user
    get:
      tags:
      - user
      description: Returns all the user docs to caller
      # used as the method name of the controller
      operationId: userGet
      parameters:
        - name: token
          in: query
          description: token to identify the user
          required: true
          type: string
      responses:
        "200":
          description: Success
          schema:
            type: array
            items: {}
    put:
      tags:
      - user
      description: Puts user specific docs to DB 
      # used as the method name of the controller
      operationId: userSet
      parameters:
        - name: token
          in: query
          description: token to identify the user
          required: true
          type: string
      responses:
        "200":
          description: Success
          schema:
            type: string
            # a pointer to a definition          
  /befunde:
    x-swagger-router-controller: befunde
    get:
      tags:
      - befunde
      description: gets befunde to caller
      # used as the method name of the controller
      operationId: befundeGet
      parameters:
        - name: token
          in: query
          description: token to identify the user
          required: true
          type: string
        - name: patientID
          in: query
          description: PatientId from the patient which befunde you try to retrieve 
          required: true
          type: string
      responses:
        "200":
          description: OK     
    put:
      tags:
      - befunde
      description: Puts user specific docs to DB 
      # used as the method name of the controller
      operationId: befundeSet
      parameters:
        - name: token
          in: query
          description: token to identify the user
          required: true
          type: string
      responses:
        "200":
          description: Success
          schema:
            type: string
            # a pointer to a definition

这是/ api / controllers中的befund.js:

module.exports = {
    befundeGet: befundeGet,
    befundeSet: befundeSet
};


function befundeGet(req,res){
        var name = 'stranger';
        console.log(req);
        console.log('Befunde get');
        var hello = util.format('Hello, %s', name);
        res.json(hello);
}

function befundeSet(req,res){

}

似乎甚至没有运行我的befundeGet函数,因为我的节点控制台在API调用后保持为空。

0 个答案:

没有答案