我正在尝试在 express 和 node 中创建一个招摇的 API 文档。这是我完成的部分代码。
index.js 文件
const express = require('express');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const http = require('http')
const app = express();
http.createServer(app).listen(3001)
console.log("Listening at:// port:%s (HTTP)", 3001)
const swaggerDefinition = {
info: {
title: 'REST API for my App', // Title of the documentation
version: '1.0.0', // Version of the app
description: 'This is the REST API for my product', // short description of the app
},
};
const options = {
// import swaggerDefinitions
swaggerDefinition,
// path to the API docs
apis: ['./docs/*.yaml'],
};
// initialize swagger-jsdoc
const swaggerSpec = swaggerJSDoc(options);
// use swagger-Ui-express for your app documentation endpoint
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
我的 .yaml 文件
host: localhost:3016
basePath: "/productRule"
schemes:
- http
consumes: []
produces: []
paths:
"/search":
post:
tags: # Tag property
- search # Value of the tag
summary: search
produces:
- application/json
parameters: # request parameters
- in: body # request body
name: search
# name of request, can be any name
description: It enables a user to create an account
required: false # can also be true depending on user preference
schema: # Schema definition
$ref: '#/definitions/search'
responses: # server responses
200:
description: An object with user details
definitions: # Schema defination for request body
search:
type: object
properties:
search:
type: object
properties:
pageSize:
type: string
sortBy:
type: string
searchQuery:
type: string
在这里,我想调用一个在端口 3016 中运行的 api。但是当我运行此脚本时,swagger 调用端口 3001,我在其中定义了 swagger 脚本,如屏幕截图所示。
你能帮我找出我哪里出错了吗?