What does multer mean in node?

WBOY
Release: 2022-04-15 15:57:25
Original
2755 people have browsed it

In node, multer is a middleware used to process "multipart/form-data" type data format, mainly used for uploading files; after parsing the request body, the middleware will send a message to the Request object Add a body object and a file or files object.

What does multer mean in node?

The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.

What does multer mean in node

Multer is a node.js middleware, used to process multipart/form-data type form data. It is mainly used to upload files. It is written on top of busboy and is very efficient.

Note: Multer will not process any non-multipart/form-data type form data

After parsing the request body, Multer will add a body object and a file or file to the Request object. files object (use the files object when uploading multiple files).

The body object contains the text fields in the submitted form (if any), and the file (or files) object contains the files uploaded through the form.

Tips: multipart/form-data is used to specify the special type of data to be transmitted, mainly the non-text content we upload, such as pictures or mp3, etc.

const express = require('express')
const multer  = require('multer')
const app = express()
const storage = multer.diskStorage({
  //保存路径
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads')
    //注意这里的文件路径,不是相对路径,直接填写从项目根路径开始写就行了
  },
  //保存在 destination 中的文件名
  filename: function (req, file, cb) {    
    cb(null, file.fieldname + '-' + Date.now())
  }
})
const upload = multer({ storage: storage })
app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file 是 `avatar` 文件的信息
  // req.body 将具有文本域数据,如果存在的话
})
app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files 是 `photos` 文件数组的信息
  // req.body 将具有文本域数据,如果存在的话
})
const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
  // req.files 是一个对象 (String -> Array) 键是文件名,值是文件数组
  // 例如:
  //  req.files['avatar'][0] -> File
  //  req.files['gallery'] -> Array
  // req.body 将具有文本域数据,如果存在的话
})
Copy after login

multer(options)

Multer accepts an options object, the most basic of which is the dest attribute, which will tell Multer where to save the uploaded file. If you omit the options object, these files will be kept in memory and never written to disk.

Usually, for general web applications, you only need to set the dest attribute, like this:

const upload = multer({ dest: 'uploads/' })
Copy after login

If you want more control when uploading, you can use the storage option instead of dest. Multer has two storage engines: DiskStorage and MemoryStorage; more engines are available from third parties.

Recommended learning: "nodejs video tutorial"

The above is the detailed content of What does multer mean in node?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template