Skip to content

Instantly share code, notes, and snippets.

@vivekyadav5750
Created January 23, 2024 05:38
Show Gist options
  • Save vivekyadav5750/a5cae69f251eac9a3753b8db312a138e to your computer and use it in GitHub Desktop.
Save vivekyadav5750/a5cae69f251eac9a3753b8db312a138e to your computer and use it in GitHub Desktop.
Nodejs Image/File Upload (use Multer Module)

Node.js Image/File Upload Example using Multer

File Upload Middleware (file-upload.middleware.js)

// file-upload.middleware.js

import multer from 'multer';

// Configure storage for uploaded files
const storageConfig = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'public/uploads'); // Destination folder for uploads
    },
    filename: (req, file, cb) => {
        const fileName = Date.now() + file.originalname; // Unique filename
        cb(null, fileName);
    }
});

// Multer middleware for handling file uploads
export const uploadFile = multer({ storage: storageConfig });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment