Gandalf the White
Gandalf the White

Reputation: 2465

Express JS Node JS multer intergration issue

https://github.com/cminhho/AngularFileUpload-Express/blob/master/server.js

I am implementing this tutorial and I have done things accordingly but when I am working with updated express js, it throws an errors on the express js side.

Error

    C:\nodefiles\new\node_modules\express\lib\application.js:209
throw new TypeError('app.use() requires middleware functions');
      ^
TypeError: app.use() requires middleware functions
    at EventEmitter.use (C:\nodefiles\new\node_modules\express\lib\application.js:209:11)
    at Object.<anonymous> (C:\nodefiles\new\server.js:9: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

Here is the code that throws error

    app.use(express.static(__dirname + '/public')); 
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;
}
}));

This is probably because of Express JS updated version please tell me what should I change in server side to make this work.

You can get all the front end code as well from the github repository.

Upvotes: 1

Views: 592

Answers (2)

Sanjay Vemuri
Sanjay Vemuri

Reputation: 32

I have tried it long time back hope it helps..

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>

<form method="post" action="http://localhost:3300/api/fileup" name="submit" enctype="multipart/form-data">
    <input type="file" name="profile"> <br /><br />
    <input type="text" name="target">  <br /><br />
    <input type="submit" name="submit" value="Submit">
</form>

</body>

</html>

===============================================


var express = require('express');
var router = express.Router();


var mongodb = require('mongodb');
var uri= require('../config/config').mongouri;
var MongoClient = mongodb.MongoClient;


var multer = require('multer');


/*
var upload = multer({

    dest: 'public/uploads/',
    limits: {fileSize: 1000000, files:1}

});
*/



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

var upload = multer({ storage: storage });


router.get('/', function(req, res)
{

    res.render('fileup');

});



router.post('/', upload.single('profile'), function (req, res) {

    res.send(req.body.target);
});






module.exports = router;

Upvotes: 0

Krzysztof Sztompka
Krzysztof Sztompka

Reputation: 7204

multer changed api. Tutorial which you follow is outdated. You can ofcourse use older version of multer, but there was some problems. In github multer page there is correct way of use upload file: https://github.com/expressjs/multer From multer page:

var express = require('express')
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })

var app = express()

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files
  // req.body will contain the text fields, if there were any
})

Upvotes: 2

Related Questions