This Express method of uploading pictures is somewhat incomprehensible? ?

  node.js, question

Code:
App.post (‘/upload’, photos.submit (app.get(‘photos’)//why does the method of handling the route pass in an (app.get (‘photos’)) this is why and why? ?

What is the so-called dir?
 return function (req, res, next) {
 var img = req.files.photo.image;  //Here are the documents
 var name = req.body.photo.name || img.name;  //here is an input to input the picture name
 var path = join(dir,img.name)
 fs.rename(img.path,path, function (err) {
 If(err) return next(err) // why call the rename method?
 
 Create (//here is a mongoose instance
 name:name,
 path:img.name
 }, function (err) {
 if(err) return next(err)
 res.redirect('/')
 })
 })
 bracket
 bracket

app.get('photos')Is to obtain the previous useApp.set('photos', value)The value set. Therefore, the first half of the code should have a code that sets this value, which means the path to save the uploaded file.

var dir = app.get("photos");
 photos.submit(dir)

This latter one returns a handler and writes directlyfunction (req,res,next){}The difference is that there is one more closure saveapp.getGets the path of the.

Upload files, express3.0 inside is that the framework will first save you to a temporary location, and then use your code to move to the location you want to save.req.filesIt stores the information of the uploaded file (size, name, temporary storage location, etc.).
fs.renameIn fact, you didn’t find out.renameAndfile.moveIn fact, it means the same thing, it means moving files.
So the conclusion is:
dirDefine the path where you want to save the uploaded picture.
renameMove the file to the location you want to save (by the way, if the user can choose a custom name)

It is better to post the code of the front-end form.