我正在使用Express和Node。我按照以下链接获取了我的API文件:http://mherman.org/blog/2016/05/26/swagger-and-nodejs/#.We8gq2iCzDc。我有相同的项目结构。我确信有些东西丢失但我无法找到错误。任何帮助表示赞赏。
这是我的app.js:
import express from 'express';
import path from 'path';
var routes = require('./routes/routes');
var swaggerJSDoc = require('swagger-jsdoc');
var bodyParser = require('body-parser');
var swaggerDefinition = {
info: {
title: 'Node Swagger API',
version: '1.0.0',
description: 'Demonstrating how to describe a RESTful API with Swagger',
},
host: 'localhost:3000',
basePath: '/',
};
var options = {
swaggerDefinition: swaggerDefinition,
apis: ['./routes/routes.js']
};
var swaggerSpec = swaggerJSDoc(options);
class Server {
constructor() {
this.app = express();
}
configureApp() {
this.app.set('port', (process.env.PORT || 3000));
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: true }));
this.app.use(bodyParser());
this.app.use(express.static(path.join(__dirname, 'public')));
this.app.use('/', routes);
}
configureCORS() {
this.app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, PUT, DELETE, GET');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
res.setHeader('Cache-Control', 'no-cache');
next();
});
}
configureRoutes() {
this.app.post('/api/doctorPost', doctor.post);
this.app.put('/api/doctorEdit/:id', doctor.put);
this.app.delete('/api/doctorDelete/:id', doctor.delete);*/
this.app.get('/swagger.json', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
});
}
listen(port) {
this.app.listen(port, () => {
console.log(`Server started: http://localhost:${port}/`);
});
}
run() {
this.configureApp();
this.configureCORS()
this.configureRoutes();
this.listen(this.app.get('port'));
}
}
export default Server;
这是我的routes.js:
var express = require('express');
var router = express.Router();
var api = require('../doctor.api');
/**
* @swagger
* definition:
* doctor:
* type: object
* properties:
* id:
* type: integer
* name:
* type: string
* picUrl:
* type: string
* briefDescription:
* type: string
* speciality:
* type: string
* exprience:
* type: string
* description:
* type: string
* contact:
* type: string
* email:
* type: string
* phoneno:
* type: integer
* status:
* type: string
* description: doctor Status
* enum:
* - available
* - pending
* - not exists
* waitingTime:
* type: integer
* format: int64
* rating:
* type: integer
* format: int64
* videoUrl:
* type: string
* appearUrl:
* type: string
* collapseId:
* type: string
* thumbnailUrl:
* type: string
* lastUpdateTime:
* type: integer
* format: int64
*/
/**
* @swagger
* /api/doctorGet:
* get:
* tags:
* - doctor
* description: Returns all doctor
* produces:
* - application/json
* responses:
* 200:
* description: An array of doctor
* schema:
* $ref: '#/definitions/doctor'
*/
router.get('/api/doctorGet', api.getDoctors);
/**
* @swagger
* /api/doctorPost:
* post:
* tags:
* - doctor
* summary: Add a new doctor to the list
* description: New doctor added
* operationId: addDoctor
* consumes:
* - application/json
* - application/xml
* produces:
* - application/xml
* - application/json
* parameters:
* - in: body
* name: body
* description: Doctor object that needs to be added to the list
* required: true
* schema:
* $ref: '#/definitions/doctor'
* responses:
* '405':
* description: Invalid input
* security:
* - doctor_auth:
* - 'write:doctor'
* - 'read:doctor'
*/
router.post('/api/doctorPost', api.createDoctor);
/**
* @swagger
* /api/doctorEdit/{id}:
* put:
* tags:
* - doctor
* description: Updates a single doctor
* produces:
* - application/json
* parameters:
* - name: id
* description: doctor's id
* in: path
* required: true
* type: integer
* - name: puppy
* description: doctor object
* in: body
* required: true
* schema:
* $ref: '#/definitions/doctor'
* responses:
* 200:
* description: Successfully updated
*/
router.put('/api/doctorEdit/:id', api.updateDoctor);
/**
* @swagger
* /api/doctorDelete/{id}:
* delete:
* tags:
* - doctor
* description: Deletes a single doctor
* produces:
* - application/json
* parameters:
* - name: id
* description: doctor's id
* in: path
* required: true
* type: integer
* responses:
* 200:
* description: Successfully deleted
*/
router.delete('/api/doctorDelete/:id', api.deleteDoctor);
module.exports = router;
我的输出是:
{" info":{" title":" Node Swagger API","版本":" 1.0.0","说明":"演示如何描述 RESTful API与Swagger"}," host":" localhost:3000"," basePath":" /",&# 34;招摇":" 2.0""路径&#34 ;: {},"定义":{},"响应":{},"参数":{}," securityDefinitions":{} }
预期输出将是这样的:
{" info":{" title":" Node Swagger API","版本":" 1.0.0","说明":"演示如何使用Swagger"来创建RESTful API。 },"主机":"本地主机:3000""基本路径":" /""招摇&#34 ;: " 2.0""路径" {" / API /小狗" {"获得" {"标记&#34 ;:[" Puppies"],"描述":"返回所有小狗","生成":[" application / json& #34;],"回复":{" 200":{"描述":"一系列小狗","架构" {" $ REF":"#/定义/小狗"}}}}"后" {"标记&#34 ;:[" Puppies"],"描述":"创建一只新的小狗","生成":[" application / json"],"参数":[{" name":" puppy"," description":" Puppy object& #34;"在":"主体""需":真,"模式" {" $ REF&# 34;:"#/定义/小狗"}}],"响应" {" 200" {" DESCRIPTIO n":"已成功创建"}}}}," / api / puppies / {id}":{" get":{&#34 ;标签":["小狗"],"描述":"返回一只小狗","产生":[&# 34;应用/ JSON&#34],"参数":[{"名称":" ID""描述":&# 34; Puppy的id"," in":" path"," required":true," type": "整数"}],"回复":{" 200":{"描述":"一只小狗" "模式" {" $ REF":"#/定义/小狗"}}}}"把" {&# 34;标签":["小狗"],"描述":"更新一只小狗","产生":[& #34;应用/ JSON&#34],"参数":[{"名称":" ID""描述":& #34; Puppy的id"," in":" path"," required":true," type" :"整数"},{"名称":"小狗","描述":" Puppy对象",& #34;在":"主体" "需":真,"模式" {" $ REF":"#/定义/小狗"}}],&# 34;回复":{" 200":{"说明":"已成功更新"}}},"删除":{ "标签":["小狗"],"描述":"删除一只小狗","产生": ["应用/ JSON&#34],"参数":[{"名称":" ID""描述" :" Puppy的id"," in":" path"," required":true," type&# 34;:"整数"}],"回复":{" 200":{"说明":"已成功删除" 34;}}}}}"定义" {"小狗" {"属性" {"名称" {&# 34;类型":"串"}"品种" {"类型":"串"}"年龄" {"类型":"整数"}"性别" {"类型":"串&# 34;}}}}"响应":{},"参数":{}," securityDefinitions":{},"标记&#34 ;:[]}