I have multer in my app.js like :
var multer = require('multer');
app.use(multer({ dest: './public/img/profile/',
rename: function (fieldname, filename) {
return fieldname;
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...')
},
limits: {
files: 1
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path)
imageUploaded=true;
}
}));
This works but I want to set these settings in a secific route. for example imageRoute.js and not in my app.js. But what should I use instead of app.use() than because I cant access app in my routes. I create routes in my app.js like:
var imageRoutes = require('./routes/imageRoutes')();
app.use('/image', imageRoutes);
imageRoutes.js
module.exports = function(passport) {
var that = this;
// Modules
var express = require('express');
var router = express.Router();
var fs = require('fs');
// Middleware: Checks if user is authenticated removed for more readable code
router.post('/fileupload', that.ensureAuthenticated, function(req, res, next) {
//setup multer here
});
router.post('/fileupload2', that.ensureAuthenticated, function(req, res, next) {
//setup multer here with a different destination path/filename etc
});
return router;
}
i've found this question but it didnt really help me.
UPDATE
I now edited my app.js with :
var multer = require('multer');
var imageRoutes= require('./routes/imageRoutes')(someRepo, multer);
imageRoutes.js
// POST: CREATE avatar
router.post('/avatar', function(req, res) {
router.use(multer({
dest: './public/img/profile/',
rename: function (fieldname, filename) {
return fieldname;
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...')
},
limits: {
files: 1
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path)
imageUploaded=true;
console.log(req.files);
res.redirect('/');
}
}))
});
I see chrome is uploading a file to 100% but then it does nothing. it dosn't log is starting.. or any other error.