File Upload Configuration

Novaxjs2 provides comprehensive file upload configuration options to handle various upload scenarios securely and efficiently.

Basic Configuration

Configure all file upload settings at once using the setFileConfig method:

// Configure file upload settings
app.setFileConfig({
  maxSize: 10,          // Max size in MB (default: 50MB)
  allowedTypes: ['image/jpeg', 'image/png', 'application/pdf'],
  maxFiles: 3,          // Max files per upload (default: 5)
  keepOriginalName: true // Keep original filenames (default: false)
});

Individual Settings

You can also configure each setting individually for more granular control:

// Set max file size (in MB)
app.setFileSizeLimit(10);

// Set allowed MIME types (empty array allows all types)
app.setAllowedFileTypes(['image/jpeg', 'image/png', 'application/pdf']);

// Set max number of files per upload
app.setMaxFiles(3);

// Keep original filenames (default uses generated names)
app.setKeepOriginalName(true);

Configuration Options

OptionTypeDescriptionDefault
maxSizenumberMaximum file size in megabytes. Files larger than this will be rejected.50 MB
allowedTypesstring[]Array of allowed MIME types. Empty array allows all types. Example: ['image/jpeg', 'image/png'][] (all types)
maxFilesnumberMaximum number of files allowed in a single upload request.5
keepOriginalNamebooleanWhether to preserve original filenames. If false, generates unique filenames.false

Complete File Upload Example

const Nova = require('novaxjs2');
const app = new Nova();

// Configure file uploads
app.setFileConfig({
  maxSize: 10,
  allowedTypes: ['image/jpeg', 'image/png', 'application/pdf'],
  maxFiles: 5,
  keepOriginalName: false
});

// File upload route
app.post('/upload', (req, res) => {
  if (!req.files || Object.keys(req.files).length === 0) {
    return res.status(400).json({ error: 'No files uploaded' });
  }
  
  // Process uploaded files
  const fileInfo = Object.values(req.files).map(file => ({
    originalName: file.originalName,
    filename: file.filename,
    size: file.size,
    mimetype: file.mimetype,
    path: file.path
  }));
  
  return res.json({ 
    message: 'Files uploaded successfully', 
    files: fileInfo,
    totalSize: fileInfo.reduce((sum, file) => sum + file.size, 0)
  });
});

// Error handling for file uploads
app.error((err, req, res) => {
  if (err.code === 'FILE_TOO_LARGE') {
    return res.status(413).json({ 
      error: 'File too large', 
      maxSize: `${app.fileConfig.maxSize}MB` 
    });
  }
  
  if (err.code === 'INVALID_FILE_TYPE') {
    return res.status(415).json({ 
      error: 'Invalid file type', 
      allowedTypes: app.fileConfig.allowedTypes 
    });
  }
  
  // Default error handling
  return res.status(500).json({ error: 'Upload failed' });
});

app.at(3000, () => console.log('Server running on port 3000'));

Uploaded File Object Structure

When files are uploaded successfully, they are available in req.files with the following structure:

{
  "fieldname": "upload",           // Field name from the form
  "originalName": "photo.jpg",     // Original filename
  "filename": "abc123.jpg",        // Saved filename (if keepOriginalName is false)
  "encoding": "7bit",              // File encoding
  "mimetype": "image/jpeg",        // MIME type
  "size": 102400,                  // File size in bytes
  "path": "/uploads/abc123.jpg"    // Path to saved file
}