UNPKG

get-express-starter

Version:

Get production ready express boilerplate with a single command

36 lines (30 loc) 912 B
const multer = require('multer'); const path = require('path'); const fs = require('fs'); const ALLOWED_FILE_SIZE = 2000000; const UPLOAD_DIR = 'src/uploads/'; const storage = multer.diskStorage({ destination: (req, file, cb) => { if (!fs.existsSync(UPLOAD_DIR)) { fs.mkdirSync(UPLOAD_DIR, { recursive: true }); } cb(null, UPLOAD_DIR); }, filename: (req, file, cb) => { cb(null, `${Date.now()}-${file.originalname}`); }, }); const upload = multer({ storage, limits: { fileSize: ALLOWED_FILE_SIZE }, fileFilter: (req, file, cb) => { const allowedTypes = /csv/; const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase()); const mimetype = allowedTypes.test(file.mimetype); if (mimetype && extname) { return cb(null, true); } cb(new Error('Error: File upload only supports CSV files!')); }, }); module.exports = upload;