0

I'm trying to implement file-upload in my express 4 based app. I follow this tutorial: https://codeforgeek.com/2014/11/file-uploads-using-node-js/

In that code:

var express = require('express');
var multer = require('multer');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.use(multer({
    dest: './uploads/',
    rename: function (fieldname, filename) {
        return filename + Date.now();
    },
    onFileUploadStart: function (file) {
        console.log(file.originalname + ' is starting ...')
    },
    onFileUploadComplete: function (file) {
        console.log(file.fieldname + ' uploaded to  ' + file.path)
        done = true;
    }
}));

The line app.use(multer({

causes the error:

C:\Development\HERA\hera_node\node_modules\express\lib\application.js:206
    throw new TypeError('app.use() requires middleware functions');
          ^
TypeError: app.use() requires middleware functions
    at EventEmitter.use (C:\Development\HERA\hera_node\node_modules\express\lib\
application.js:206:11)
    at Object.<anonymous> (C:\Development\HERA\hera_node\app.js:17:5)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

What could be the cause? I'm very new in node & express. The explanation requires middleware functions doesn't tell me anything.

2
  • It looks like the multer setup procedure has changed, see this. Or, if that's a bit much, you could install the version that your tutorial is using: npm install [email protected]. Commented Jul 30, 2015 at 17:26
  • Great! npm install [email protected] worked! Would you mind posting it as the answer? I'd accept it. Commented Jul 31, 2015 at 11:22

2 Answers 2

2

The tutorial is using an older version of Multer (v0.1.6 to be exact), where the current version of Multer is 1.0.1. There has been a change in how you need to set up and use Multer in between those versions, which is why the tutorial code doesn't work anymore with the latest Multer.

A quick fix would be to install the older version:

$ npm install [email protected]

However, at some point you probably want to move to the most recent version of Multer.

Sign up to request clarification or add additional context in comments.

2 Comments

"However, at some point you probably want to move to the most recent version of Multer." - yes, I will. currently I'm a very beginner in node.
@ValentinHeinitz once you get the file upload working properly and you have a bit more experience upgrading will probably be easier (instead of using the latest version straight away) =D
0

//run java-script file and open the view page and then upload the files and submit//

  • js file

const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();
let date = require('date-and-time');

// default options
app.use(fileUpload());

app.post('/upload', function(req, res) {
  if (!req.files)
    return res.status(400).send('No files were uploaded.');

  // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
  let sampleFile = req.files.sampleFile;
  let now = new Date();
  var now1 = date.format(now, 'YYYY_MM_DD_HH_mm_ss');
  var desired = now1.replace(/[^\w\s]/gi, '')
  var name = now1 + "_" + req.files.sampleFile.name;
  // Use the mv() method to place the file somewhere on your server
  sampleFile.mv("./../nodejs_image_upload_example/Images/" + name, function(err) {
    if (err)
      return res.status(500).send(err);

    res.send('File uploaded!');
  });
});


app.listen(2000, function(a) {
  console.log("Listening to port 2000");
});
<html>

<body>
  <form ref='uploadForm' id='uploadForm' action='http://localhost:2000/upload' method='post' encType="multipart/form-data">
    <input type="file" name="sampleFile" />
    <input type='submit' value='Upload!' />
  </form>
</body>

</html>

`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.