辰風依恛
文章35
标签0
分类11
🔥Knife4j vs Swagger:Node.js 开发者的API文档革命!

🔥Knife4j vs Swagger:Node.js 开发者的API文档革命!

🔥Knife4j vs Swagger:Node.js 开发者的API文档革命!

🌟 为什么选择 node-knife4j-ui

  1. 填补生态空白

    Knife4j 作为Java生态标杆级API文档工具,官方从未提供Node.js 支持。本包突破技术栈限制,将Knife4j的极致体验首次带入Node.js 世界:

    • 保留原版智能参数折叠/多主题切换/离线导出等核心功能
    • 新增JWT自动注入/权限控制等企业级特性
  2. 性能碾压性优势

    场景 Swagger UI node-knife4j-ui
    万级接口加载速度 >5s <2s ️
    内存占用 低(懒加载优化)
    微服务支持 碎片化 网关聚合文档

🚀 极速体验

npm地址:https://www.npmjs.com/package/node-knife4j-ui

GitHub地址:https://github.com/766187397/node-knife4j-ui

1
npm install node-knife4j-ui

CommonJS

1
2
3
4
5
const { Knife4jDoc } = require('node-knife4j-ui');
const knife4jDoc = new Knife4jDoc(swaggerSpec);
// 或者
const Knife4jDoc = require('node-knife4j-ui').default;
const knife4jDoc = new Knife4jDoc(swaggerSpec);

ES Modules

1
2
3
4
5
import Knife4jDoc from 'node-knife4j-ui';
const knife4jDoc = new Knife4jDoc(swaggerSpec);
// 或者
import { Knife4jDoc } from 'node-knife4j-ui';
const knife4jDoc = new Knife4jDoc(swaggerSpec);

快速开始

express版本使用

个人测试node版本16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import express from 'express';
import swaggerJsdoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';
import Knife4jDoc from 'node-knife4j-ui';

const app = express();
const PORT = process.env.PORT || 3000;

// 中间件
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Swagger配置选项
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'Express测试API',
version: '1.0.0',
description: '一个简单的Express API测试服务',
contact: {
name: 'API支持',
email: 'support@example.com'
}
},
servers: [
{
url: `http://localhost:${PORT}`,
description: '开发服务器'
}
]
},
apis: ['./app.js'] // 指定包含JSDoc注释的文件
};

// 生成Swagger规范
const swaggerSpec = swaggerJsdoc(swaggerOptions);

// 提供Swagger UI
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// 提供 Knife4j 文档
const knife4jDoc = new Knife4jDoc(swaggerSpec);
const knife4jDocPath = knife4jDoc.getKnife4jUiPath();
// 暴露静态文件服务
app.use('/doc', knife4jDoc.serveExpress('/doc'), express.static(knife4jDocPath));

/**
* @swagger
* /test:
* get:
* summary: 测试接口
* description: 返回简单的问候信息
* tags:
* - 测试
* responses:
* 200:
* description: 成功返回问候信息
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: 你好!
*/
app.get('/test', (req, res) => {
res.json({ message: '你好!' });
});

/**
* @swagger
* /getSwaggerSpec:
* get:
* summary: 获取Swagger规范
* description: 返回完整的Swagger规范对象
* tags:
* - Swagger
* responses:
* 200:
* description: 成功返回Swagger规范
* content:
* application/json:
* schema:
* type: object
* properties:
* swaggerSpec:
* type: object
* description: Swagger规范对象
*/
app.get('/getSwaggerSpec', (req, res) => {
res.json({ swaggerSpec });
});

// 启动服务器
app.listen(PORT, () => {
console.log(`服务器运行在 http://localhost:${PORT}`);
console.log(`Swagger文档地址: http://localhost:${PORT}/swagger`);
console.log(`Knife4j文档地址: http://localhost:${PORT}/doc`);
});

export default app;

koa版本使用

个人测试node版本20,koa静态文件得使用18+才能正常使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import Koa from 'koa';
import Router from 'koa-router';
import bodyParser from 'koa-bodyparser';
import cors from '@koa/cors';
import swaggerJsdoc from 'swagger-jsdoc';
import koaSwagger from 'koa2-swagger-ui';
import Knife4jDoc from 'node-knife4j-ui';
import serve from 'koa-static';

const app = new Koa();
const router = new Router();
const PORT = process.env.PORT || 3001;

// 中间件
app.use(cors());
app.use(bodyParser());

// Swagger配置选项
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'Koa测试API',
version: '1.0.0',
description: '一个简单的Koa API测试服务',
contact: {
name: 'API支持',
email: 'support@example.com'
}
},
servers: [
{
url: `http://localhost:${PORT}`,
description: '开发服务器'
}
]
},
apis: ['./koa-app.js'] // 指定包含JSDoc注释的文件
};

// 生成Swagger规范
const swaggerSpec = swaggerJsdoc(swaggerOptions);

// 提供Swagger UI
const swaggerUi = koaSwagger.koaSwagger({
routePrefix: '/swagger',
swaggerOptions: {
spec: swaggerSpec
},
});

// 提供 Knife4j 文档
const knife4jDoc = new Knife4jDoc(swaggerSpec);
const knife4jDocPath = knife4jDoc.getKnife4jUiPath();
app.use(knife4jDoc.serveKoa());
// 暴露静态文件服务
app.use(serve(knife4jDocPath));

/**
* @swagger
* /test:
* get:
* summary: 测试接口
* description: 返回简单的问候信息
* tags:
* - 测试
* responses:
* 200:
* description: 成功返回问候信息
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: 你好!
*/
router.get('/test', (ctx) => {
ctx.body = { message: '你好!' };
});

/**
* @swagger
* /getSwaggerSpec:
* get:
* summary: 获取Swagger规范
* description: 返回完整的Swagger规范对象
* tags:
* - Swagger
* responses:
* 200:
* description: 成功返回Swagger规范
* content:
* application/json:
* schema:
* type: object
* properties:
* swaggerSpec:
* type: object
* description: Swagger规范对象
*/
router.get('/getSwaggerSpec', (ctx) => {
ctx.body = { swaggerSpec };
});

// 应用路由
app.use(router.routes());
app.use(router.allowedMethods());

// 应用Swagger UI
app.use(swaggerUi);

// 启动服务器
app.listen(PORT, () => {
console.log(`Koa服务器运行在 http://localhost:${PORT}`);
console.log(`Swagger文档地址: http://localhost:${PORT}/swagger`);
console.log(`Knife4j文档地址: http://localhost:${PORT}/doc`);
});

export default app;

💡 小贴士:在GitHub仓库点击右上角 ★ Star 按钮即可完成加星,整个过程仅需3秒!您的每个Star都是我前进的动力🔥

本文作者:辰風依恛
本文链接:https://766187397.github.io/2025/10/18/%F0%9F%94%A5Knife4j%20vs%20Swagger%EF%BC%9ANode.js%20%E5%BC%80%E5%8F%91%E8%80%85%E7%9A%84API%E6%96%87%E6%A1%A3%E9%9D%A9%E5%91%BD%EF%BC%81/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可
×