简体   繁体   中英

How to integrate swagger with restify framwork in node

I implemented a project for test purpose with restify framework in node and implemented a GET API. But I don't know how to integrate a swagger with restify framework. There are many blogs for integration swagger with express.. I followed a link like

Please help me how to integrate.

For everyone who's come this far, as well as me, I assume you're using restify instead of express and haven't found an easy answer yet. I have an API server using restify, use typescript to program and convert the files to javascript before running my server, all in a Docker container. After a lot of searching I managed to solve my problem as follows:

  1. Install the "swagger-autogen" package.

  2. Create a file called "swagger.ts" with the following commands:

const swaggerAutogen = require('swagger-autogen')()
const outputFile = '../swagger_output.json'

const endpointsFiles = ['../routes/users.router.ts'] // root file where the route starts.

swaggerAutogen(outputFile, endpointsFiles)
    .then(() => {
        require('./dist/src/app.js') // Your project's root file
})
  1. Go to the files reserved for your API routes (where they have the GET, POST, PATCH, ... functions) -- in my case '../routes/users.router.ts', and put comments, like :
// #swagger.path = "/users"
// #swagger.tags = ['User']
// #swagger.description = 'Endpoint to create a user.'
/*
#swagger.responses[201] = {
  schema: { "$ref": "#/definitions/User" },
  description: "User was successfully created." }
*/

NOTE: To learn more about these swagger-autogen comment tags see: https://github.com/davibaltar/swagger-autogen#swagger-20

  1. Run the following command: (In my case, this command is in a script in package.json that is called inside the Dockerfile):

NOTE: Remember that if you don't automatically generate the javascript files from the script in typescript you need to create your structure entirely in javascript and not in typescript. Pay attention to file names and extensions.

node ./dist/src/server/swagger.js // change to your path

When you do this a swagger_output.json file will be created containing your application details.

  1. After the file has been generated, you need to install the "swagger-ui-restify" package.

  2. Put these commands where the middlewares are (usually at application startup):

const swaggerUi = require("swagger-ui-restify")
const swaggerDocument = require("../swagger_output.json")

const swaggerOptions = {
                    explorer: true,
                    baseURL: 'api-docs',
                }

app.get("/api-docs"+'/*', ...swaggerUi.serve)
app.get("/api-docs", swaggerUi.setup(swaggerDocument, swaggerOptions))

Now your application has a middleware to consult the automatically generated documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM