So according to the error message at the bottom, the problem occurs on line 11, which is where I give multer some parameters. As it looks like there shouldn’t be any problems, I initially thought multer wasn’t installed properly, but all the multer files (I believe) are in place under node_modules. Now I’m clueless as to what the problem might me.
var express = require('express');
var app = express();
var fs = require('fs');
var multer = require('multer');
var bodyParser = require('body-parser');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/tmp/' }));
app.get('/index.html', function (req, res) {
res.sendFile( __dirname + "/" + "index.html" );
})
app.post('/file_upload', function (req, res) {
console.log(req.files.file.name);
console.log(req.files.file.path);
console.log(req.files.file.type);
var file = __dirname + "/" + req.files.file.name;
fs.readFile( req.files.file.path, function (err, data) {
fs.writeFile(file, data, function (err) {
if( err ) {
console.log( err );
} else {
response = {
message:'File uploaded successfully',
filename:req.files.file.name
};
}
console.log( response );
res.end( JSON.stringify( response ) );
});
});
})
var server = app.listen(8080, function () {
var host = server.address().address
var port = server.address().port
console.log("Post app listening to://%s:%s", host, port)
})
TypeError: app.use() requires a middleware function
at EventEmitter.use (/home/asgeir/nodejs/first_test_app/node_modules/express/lib/application.js:210:11)
at Object.<anonymous> (/home/asgeir/nodejs/first_test_app/server.js:11:5)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
/home/asgeir/nodejs/first_test_app/node_modules/express/lib/application.js:210
throw new TypeError('app.use() requires a middleware function')
^
2
Answers
It seems that you’ve got a problem with multer.
In reading the doc, you don’t implement correctly the integration.
Look at this extract of doc: https://github.com/expressjs/multer#usage
You’ve to customize deplendling of your needs…
Try
Please refer to this question https://stackoverflow.com/a/36020740/2558788