Доброго времени суток.
При вызове маршрута:
router.post('/', upload, controller.app)
возникает следующая ошибка:
Error: Route.post() requires a callback function but got a [object Object]
router/app.js
const express = require('express')
const upload = require('../middleware/upload')
const controller = require('../controllers/app')
const router = express.Router()
router.post('/', upload, controller.app)
module.exports = router
middleware/upload.js
const multer = require('multer')
const aws = require('aws-sdk')
const multerS3 = require('multer-s3')
const Events = require('../models/Events')
const errorHandler = require('../utils/errorHandler')
const keys = require('../config/keys')
const s3FileURL = keys.AWS_URL;
aws.config.update({
secretAccessKey: keys.ACCESS_KEY,
accessKeyId: keys.ACCESS_KEY_ID,
region: keys.REGION
})
const s3 = new aws.S3();
const upload = multer({
storage: multerS3({
s3: s3,
bucket: 'verstesting',
acl: 'public-read',
key: async function (req, file, cb) {
const today = new Date().toISOString().slice(0, 10);
const path = "files.devices/" + req.body.id + "/" + today + "/" + file.originalname + ".jpg";
cb(null, path);
try {
const updateFile = await Events.findOneAndUpdate(
{rid: req.body.rid},
{$set: { filename: s3FileURL+path }}
)
res.status(200).json({
message: 'ok'
})
} catch (e) {
errorHandler(res, e)
}
}
})
}).any();
module.exports.upload = multer({upload})
controllers/app'
const mongoose = require('mongoose')
const User= require('../models/User')
const errorHandler = require('../utils/errorHandler')
module.exports.app = async function(req, res){
switch(req.body.user) {
case 'email':
const userEmail = await User.findOne({email: req.body.email}).select('_id')
if (userEmail) {
res.status(200).json({
message: 'User find'
})
}
}
}
Как я понимаю, криво определяется middleware: upload.
Как избавиться от этой ошибки?
Буду благодарен любой критике и советам по коду!