V
V
Valery Orlov2019-01-17 22:38:57
JavaScript
Valery Orlov, 2019-01-17 22:38:57

How to put multer into a separate file and load it into router?

Good day! Here is an example code:

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);
}

How can I put the aws-sdk, multer and multer-s3 modules together with their functionality into a separate file, and in the router's routes, just connect via upload.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Batukhtin, 2019-01-17
@lungdesire29

Copy to another file, do there export const upload = multer({..., and here - const upload = require("другойфайл");?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question