图片上传接口

module.exports = app => {
  const path = require('path')
  const multer = require('multer')
  const upload = multer({
    dest: path.join(__dirname, 'uploads') // 静态资源地址
  })

  app.post('/admin/api/upload', upload.single('file'), async (req, res) => {
    const file = req.file
    file.url = `http://localhost:3000/uploads/${file.filename}`
    res.send(file)
  })
}

 

Multer 是一个 node.js 中间件,用于处理 multipart/form-data 类型的表单数据,它主要用于上传文件。它是写在 busboy 之上非常高效。

注意: Multer 不会处理任何非 multipart/form-data 类型的表单数据。

posted @ 2020-07-03 21:15  尾野  阅读(1143)  评论(0)    收藏  举报