Skip to content

Instantly share code, notes, and snippets.

@kaungmyatlwin
Created June 12, 2020 17:43
Show Gist options
  • Save kaungmyatlwin/c9fa34fe669e2ba6d195696649280a77 to your computer and use it in GitHub Desktop.
Save kaungmyatlwin/c9fa34fe669e2ba6d195696649280a77 to your computer and use it in GitHub Desktop.
const router = require('express').Router();
const upload = require('./upload')('./cats');
router.post('/', upload.array('photos', 10), (req, res) => {
// handle logic
res.send('ok');
});
module.exports = router;
const router = require('express').Router();
const upload = require('./upload')('./dogs');
router.post('/', upload.array('photos', 10), (req, res) => {
// handle logic
res.send('ok');
});
module.exports = router;
const multer = require('multer');
function customStore(dirPath) {
return multer.diskStorage({
destination: (req, file, cb) => {
cb(null, dirPath);
},
filename: (req, file, cb) => {
const fname = `${Date.now()}-${encodeURIComponent(file.originalname)}`;
cb(null, fname);
},
});
}
const upload = (dirPath) => multer({
storage: customStore(dirPath),
});
module.exports = upload;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment