0
我想通過模塊化我的multer
模塊功能來清理我的控制器,這些功能允許我使用表單上傳多個文件。下面介紹的當前代碼可以工作,但我想將Muller函數放在一個新文件中。我認爲只是簡單地使用該函數並從另一個文件導出它可能會起作用,但由於某種原因,我在我的表單POST期間打了個時間,儘管在multer
代碼的開頭調用了console.log
。看起來代碼中的某些內容正在停止該進程,但不會引發錯誤消息。有沒有人看到可能導致錯誤的原因?模塊化Multer功能問題
var aws = require('aws-sdk');
var multer = require('multer');
var multerS3 = require('multer-s3');
var moment = require('moment');
var uploadDate = new moment().format("YYYY-MM-DD");
var s3 = new aws.S3();
var options = {
Bucket: process.env.AWS_BUCKET,
Expires: 60
};
if(app.get('env') === 'production' || app.get('env') === 'staging'){
options.ACL = 'private'
} else {
options.ACL = 'public-read'
};
//////////Multer code being called
var upload = multer({
storage: multerS3({
s3: s3,
bucket: options.Bucket,
contentType: multerS3.AUTO_CONTENT_TYPE,
acl: options.ACL,
key: function(req, file, cb){
var fileNameFormatted = file.originalname.replace(/\s+/g, '-').toLowerCase();
cb(null, req.session.organizationId + '/' + uploadDate + '/' + fileNameFormatted);
}
}),
fileFilter: function(req, file, cb){
if(!file.originalname.match(/\.(jpg|jpeg|png|gif|csv|xls|xlsb|xlsm|xlsx)$/)){
return cb('One of your selected files is not supported', false);
}
cb(null, true);
}
}).array('fileUpload', 5);
///////Post route calling multer and rest of form submission
.post(function(req, res){
upload(req, res, function(){
if(err){
console.log('Multer upload error');
req.flash('error', err);
res.redirect(req.get('referer'));
return;
}
... MORE CODE ...
})
});
/controllers/multer.js,其中包含multer代碼:
var express = require('express');
var app = express();
var aws = require('aws-sdk');
var multer = require('multer');
var multerS3 = require('multer-s3');
var moment = require('moment');
var s3 = new aws.S3();
var uploadDate = new moment().format("YYYY-MM-DD");
var options = {
Bucket: process.env.AWS_BUCKET,
Expires: 60,
//ContentType: req.query.file_type
};
if(app.get('env') === 'production' || app.get('env') === 'staging'){
options.ACL = 'private'
} else {
options.ACL = 'public-read'
};
module.exports = {
//Annotation file upload
annotationFileUpload: function(){
multer({
storage: multerS3({
s3: s3,
bucket: options.Bucket,
contentType: multerS3.AUTO_CONTENT_TYPE,
acl: options.ACL,
key: function(req, file, cb){
console.log("key called");
var fileNameFormatted = file.originalname.replace(/\s+/g, '-').toLowerCase();
cb(null, req.session.organizationId + '/' + uploadDate + '/' + fileNameFormatted);
}
}),
fileFilter: function(req, file, cb){
console.log("file filter called");
if(!file.originalname.match(/\.(jpg|jpeg|png|gif|csv|xls|xlsb|xlsm|xlsx)$/)){
return cb('One of your selected files is not supported', false);
}
cb(null, true);
}
}).array('fileUpload', 5);
}
}
導入/controller/multer
,並呼籲路線:
Multer路由文件中調用
var multerFile = require('./components/multer');
.post(function(req, res){
multerFile.annotationFileUpload(req, res, function(err){
if(err){
console.log('Multer upload error');
req.flash('error', err);
res.redirect(req.get('referer'));
return;
}
....MORE CODE....
})
});
感謝這個偉大的抓@mscdex!我很感激! – cphill