body-parser and multer

The new version of nodejs the body-parser and multer middleware rewritten, and many tutorials are old and this was a few days to see the article, probably meaning that, with respect to node of a tutorial, just listed out of date Yes, this is js, new technologies, a variety of new plug-in is simply the update rate !!!!
I'll sort out the latest version of the usage of these two plug-ins, as detailed tutorial is not found, only npmjs able to view the document on top of a probably a lot of new students to see the full article in English to feel big head. but as long as you are determined to see it, you will find, in fact, and watching Chinese tutorial is no different.

Look at multer, it introduced the first sentence is: on a multipart form-data upload middleware / (where multipart / form-data is a way to browse through the form to upload a file, such as: e-mail attachments, about it Baidu knowledge can, without too much explanation here.), is mainly used to upload files

installation

npm install --save multer

use

Multer will body, or file / files objects, add objects to the request object .body contains data submitted in the form of text, file / files object contains the files you upload through the form.
Examples of some use

var express = require('express')var multer = require('multer')var upload = multer({dest:'upload/'})/*几乎在所有的web app中,dest属性都是必须的,一般它的使用形式如上. */var app = express()

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file 
  // .single只能接收一个文件,文件将会被保存在req.file中,下同})

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files 
  // .array可以接受一组文件,如果文件数超过12个就会报错
  })
  var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
  // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files 
  // 
  // e.g. 
  // req.files['avatar'][0] -> File 
  // req.files['gallery'] -> Array 
  // 
  // req.body will contain the text fields, if there were any })

If you want to upload your files more operations, storage can be used instead of dest.Multer provides two storage engines
1. DiskStorage

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
})
var upload = multer({ storage: storage })

destination: file upload destination path, though not configure this parameter, the file will be automatically uploaded to the operating system's default tem / uploads folder but in order to avoid unnecessary trouble, be sure to write on their own to know the path.
filename: file after uploading the name of the server side, it is recommended renamed, this renaming filename function is in the back of the inside of the handle, he did avoid some active attacks 

2. MemoryStorage
the name suggests, this will save the file in memory, as Buffer objects, he does not have any optional parameters
var storage = multer.memoryStorage()
var upload = multer({ storage: storage })

body-parser

urlencoded ({extended: true}) ; returns a parsed url, url must be parsed utf-8 format.
, kv returns a key-value pair, if it is false, it returns a string or array if extended. is true, any type is returned

Reference from

https://www.npmjs.com/package/multer

Reproduced in: https: //my.oschina.net/boogoogle/blog/546124

Guess you like

Origin blog.csdn.net/weixin_33697898/article/details/92043737