TypeError: app.use() requires a middleware function #972
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
/eslint-disable/
var express = require('express');
var multer = require('multer');
var fs = require('fs');
var app = express();
var DIR = './uploads/';
var upload = multer({dest: DIR});
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.use(multer({
dest: DIR,
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);
}
}));
app.get('/api', function (req, res) {
res.end('file catcher example');
});
app.post('/api', function (req, res) {
upload(req, res, function (err) {
if (err) {
return res.end(err.toString());
}
});
});
var PORT = process.env.PORT || 3001;
app.listen(PORT, function () {
console.log('Working on port ' + PORT);
});
hello did you solve this problem ?
Because I had the same issue and I just couldn't solve this problem and it really did freaked me out. I tried different version of multer, no luck.
So I switched to express-fileupload. If you want here is my code with which I successfully upload file. (I just need to upload one file at a time). Hope it helps. Also, if you are able to solve this problem with multer, would you share the solution. Thanks.