安装express-formidable
npm install express-formidable
使用说明地址: https://www.npmjs.com/package/express-formidable
官方使用例子:
const express = require('express');
const formidableMiddleware = require('express-formidable');
var app = express();
app.use(formidableMiddleware());
app.post('/upload', (req, res) => {
req.fields; // contains non-file fields
req.files; // contains files
});
如果想要只再某个接口中使用该中间件:
const formidable = require('express-formidable') // 引入
router.post('/upload',formidable(), async function(req,res,next){
// 文件
console.log(req.files);
// 键值对
console.log(req.fields);
}