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 }));
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'] };
const swaggerSpec = swaggerJsdoc(swaggerOptions);
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
const knife4jDoc = new Knife4jDoc(swaggerSpec); const knife4jDocPath = knife4jDoc.getKnife4jUiPath();
app.use('/doc', knife4jDoc.serveExpress('/doc'), express.static(knife4jDocPath));
app.get('/test', (req, res) => { res.json({ message: '你好!' }); });
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;
|