网站文章内容一键排版功能,徐州如何提高网站建设,加盟微信小程序代理,做网站一月工资使用 multer 中间件来处理文件上传#xff0c;同时将文件的元数据存储到 MongoDB 中。 
一、安装依赖 
npm install multer 
二、核心代码 
// 定义文件模型const fileSchema  new mongoose.Schema({originalname: String,mimetype: String,size: Number,path: String,});cons…使用 multer 中间件来处理文件上传同时将文件的元数据存储到 MongoDB 中。 
一、安装依赖 
npm install multer 
二、核心代码 
// 定义文件模型const fileSchema  new mongoose.Schema({originalname: String,mimetype: String,size: Number,path: String,});const File  mongoose.model(File, fileSchema);// 配置 multerconst storage  multer.diskStorage({destination: function (req, file, cb) {cb(null, uploads/);},filename: function (req, file, cb) {cb(null, Date.now()  path.extname(file.originalname));},});const upload  multer({ storage: storage });// 创建上传目录const fs  require(fs);if (!fs.existsSync(uploads)) {fs.mkdirSync(uploads);}// 处理文件上传app.post(/upload, upload.single(file), async (req, res)  {try {const { originalname, mimetype, size, path }  req.file;const newFile  new File({originalname,mimetype,size,path,});await newFile.save();res.status(200).json({ message: 文件上传成功, file: newFile });} catch (error) {res.status(500).json({ message: 文件上传失败, error: error.message });}});