Доброго времени суток! Вот пример кода:
const Authentication = require('./controllers/authentication');
const passportService = require('./services/passport');
const passport = require('passport');
const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
const requireAuth = passport.authenticate('jwt', {session: false});
const requireSignIn = passport.authenticate('local', {session: false});
aws.config.update({
secretAccessKey: "Ks76SdlSqisd87JkldJc/wT",
accessKeyId: "D7F8UJ23KDJ23D9",
region: "us-east-1"
});
const s3 = new aws.S3();
const upload = multer({
storage: multerS3({
s3: s3,
bucket: 'verstesting',
acl: 'public-read',
key: function (req, file, cb) {
let today = new Date().toISOString().slice(0, 10);
var baseUrl = 'https://verstesting.s3.amazonaws.com/';
var path = "files.devices/" + req.body.aid + "/" + today + "/" + file.originalname;
cb(null, path);
Events.update({ rid: req.body.rid }, { $set: {
filename: baseUrl+path,
} })
.exec()
.then(result => {
//console.log('Запись ссылки: ', req.body.rid ,' -> ', path);
})
.catch(err => {
console.log(err);
});
}
})
}).any();
module.exports = function(app) {
app.post('/signin', requireSignIn, Authentication.signin);
app.post('/signup', upload, Authentication.signup);
app.post('/changepassword', requireAuth, Authentication.changePassword);
app.post('/editinfo', requireAuth, Authentication.editInfo);
}
Каким образом можно вынести в отдельный файл модули aws-sdk, multer и multer-s3 вместе с их функционалом, а в маршрутах router'a, просто подключить через
upload
.