Cannot app.use(multer). “requires middleware function” error

前端 未结 7 1111
滥情空心
滥情空心 2020-11-29 06:23

I\'m just starting learning NodeJS and I am stuck with a problem. I would like to upload files to my server. To do so I searched and found out this module multer. Doing as t

相关标签:
7条回答
  • 2020-11-29 06:46

    Changing to app.use(multer({dest:'./uploads/'}).single('photo')); in the app.js works for me to start the server

    0 讨论(0)
  • 2020-11-29 06:48

    You need to use app.use(multer({dest:'./uploads/'})) in the form of one of these:

    app.use(multer({dest:'./uploads/'}).single(...));
    app.use(multer({dest:'./uploads/'}).array(...));
    app.use(multer({dest:'./uploads/'}).fields(...));
    

    ie:

    app.use(multer({dest:'./uploads/'}).single('photo'));
    

    And be sure to have something like:

    <form action="/postPhotos" enctype="multipart/form-data" method="post">
        <input type="file" name="photo">
        <input type="submit" value="Upload photo">
    </form>
    

    In your html.

    0 讨论(0)
  • 2020-11-29 07:02

    The answer from @127.0.0.1 is correct, but in case you are using Express Router, the code changes a little bit:

    var express = require('express');
    var multer = require('multer');
    
    var router = express.Router();
    
    var uploads = multer({
      dest: 'public/uploads/'
    });
    
    router.post('/upload', uploads.single('avatar'), function(req, res, next) {
      console.log(req.file);
    
      //...
    });
    

    And the important bit, the form encoding should be enctype="multipart/form-data" as already said, like that:

    <form action="/upload" enctype="multipart/form-data" method="post">
        <input type="file" name="avatar">
        <input type="submit" value="Go avatar go!">
    </form>
    
    0 讨论(0)
  • 2020-11-29 07:06

    You can't change the file name using multer but you can use Node.js stream and fs module to copy the content of already uploaded file to new file(set as original file name) on same folder and delete old one.

    First of all import fs, path and multer in your node script.

    var express = require('express');
    var multer = require('multer');
    var fs = require('fs');
    var pathModule = require('path');
    

    Now, set destination directory of any type of files using multer as below.

    var app = express();
    app.use(multer({dest:__dirname+'/resoucres/'}).any());
    

    Now use stream and fs to resolve your issue.

    app.post('/uploadImage', function(request, response) {
        var readerStream = fs.createReadStream(request.files[0].path);
        var dest_file = pathModule.join(request.files[0].destination, request.files[0].originalname);
        var writerStream = fs.createWriteStream(dest_file);
    
        var stream = readerStream.pipe(writerStream);
        stream.on('finish', function(){
            fs.unlink(request.files[0].path);
        });
    });
    
    0 讨论(0)
  • 2020-11-29 07:09
    var app = require('express');
    
    var multer = require('multer');
    
    app=express();
    
    app.use(multer({dest:__dirname+'/file/uploads/'}).any());
    
    app.post('/upload',function(req,res){
    
        console.log(req.files);
    
        res.redirect('/');
    
    });
    
    0 讨论(0)
  • 2020-11-29 07:09

    Since version 1.0.0

    var upload = multer({ dest: 'tmp/' });
    app.post('/file_upload', upload.single('photo'), function (req, res) {
    
    0 讨论(0)
提交回复
热议问题