所以我试图创建一个带有上传文件端点的nodejs应用程序,但似乎我研究的任何东西,并使用chatGPT,它不会显示任何“选择文件”按钮,而只是一个输入文本框。
我尝试过下面的代码示例,但它没有显示任何按钮。 http://localhost:3000/api-docs/
const express = require('express');
const multer = require('multer');
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const app = express();
const port = process.env.PORT || 3000;
// Set up Multer for handling file uploads
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
// Swagger configuration
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'File Upload API',
version: '1.0.0',
},
},
apis: ['./server.js'], // Specify the file where your routes are defined
};
const swaggerSpec = swaggerJsdoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
/**
* @swagger
* /upload:
* post:
* summary: Upload a file
* consumes:
* - multipart/form-data
* parameters:
* - in: formData
* name: file
* type: file
* required: true
* description: The file to upload
* responses:
* 200:
* description: File uploaded successfully
*/
app.post('/upload', upload.single('file'), (req, res) => {
const file = req.file;
if (!file) {
return res.status(400).json({ message: 'No file uploaded' });
}
// Process the file (you can save it, manipulate it, etc.)
// For now, we'll just return some information about the uploaded file
res.json({
message: 'File uploaded successfully',
originalname: file.originalname,
mimetype: file.mimetype,
size: file.size,
});
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
您应该在模式中提及类型
/**
* @swagger
* /upload:
* post:
* summary: Upload a file
* consumes:
* - multipart/form-data
* parameters:
* - in: formData
* name: file
* schema:
* type: file
* required: true
* description: The file to upload
* responses:
* 200:
* description: File uploaded successfully
*/