【发布时间】:2021-05-15 22:03:55
【问题描述】:
我有一个基本的 nodejs restify 服务器,它有两种简单的方法,一种是 GET,一种是 POST。我正在尝试在我的 restify 服务之上添加 swagger API 文档。找到了对 express 的支持。
还发现了一些库 https://www.npmjs.com/package/swagger-restify 。 但不知道如何在代码中使用它。如何以某种方式添加它,我的所有 api 文档都将出现在 'http://localhost:5000/docs' 或类似的东西中。
我的基本 restify 代码如下。
var restify=require('restify');
var restifyPlugins = require('restify-plugins');
var cors = require('cors');
var server=restify.createServer({name:'test'});
server.use(restifyPlugins.acceptParser(server.acceptable));
server.use(restifyPlugins.queryParser());
server.use(restifyPlugins.fullResponse());
server.use(restifyPlugins.bodyParser({
maxBodySize: 0,
multiples: true
}));
server.use(cors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials:'false',
optionsSuccessStatus: 200 /* some legacy browsers (IE11, various SmartTVs) choke on 204 */ ,
}))
server.use(restifyPlugins.authorizationParser());
server.get({path:'/test'},function(req,res,next){
console.log("TEST API")
res.send("hello");
});
server.post({path:'/postCheck'},function(req,res,next){
console.log("TEST post API",req.body.userId)
res.send("hello post");
});
server.listen(5000,function(){
console.log("Starting server at :%s,%s",server.url,server.name)
})
【问题讨论】: