Valentin H
Valentin H

Reputation: 7458

Using file-upload module multer in node+express

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.

Upvotes: 0

Views: 2340

Answers (2)

R.V.Vighnesh
R.V.Vighnesh

Reputation: 1

//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>

`

Upvotes: 0

robertklep
robertklep

Reputation: 203554

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.

Upvotes: 2

Related Questions