0
0
Expressframework~10 mins

Storing files on disk vs memory in Express - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set up multer to store uploaded files in memory.

Express
const multer = require('multer');
const storage = multer.[1]();
const upload = multer({ storage });
Drag options to blanks, or click blank then click option'
AdiskStorage
BmemoryStorage
CfileStorage
DbufferStorage
Attempts:
3 left
💡 Hint
Common Mistakes
Using diskStorage when memoryStorage is needed.
Confusing storage options names.
2fill in blank
medium

Complete the code to configure multer to save files on disk in the 'uploads/' folder.

Express
const multer = require('multer');
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '[1]');
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname);
  }
});
const upload = multer({ storage });
Drag options to blanks, or click blank then click option'
Atemp/
Bfiles/
Cmemory/
Duploads/
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent folder name.
Confusing memory storage folder with disk storage folder.
3fill in blank
hard

Fix the error in this code to access the uploaded file buffer when using memory storage.

Express
app.post('/upload', upload.single('file'), (req, res) => {
  const fileBuffer = req.file.[1];
  res.send('File received');
});
Drag options to blanks, or click blank then click option'
Abuffer
Boriginalname
Cfilename
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access file path when using memory storage.
Using filename or originalname instead of buffer.
4fill in blank
hard

Fill both blanks to create a multer disk storage that saves files with a timestamp prefix in 'files/' folder.

Express
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '[1]');
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + '-' + file.[2]);
  }
});
Drag options to blanks, or click blank then click option'
Afiles/
Boriginalname
Cfilename
Duploads/
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong folder names.
Using 'filename' instead of 'originalname' for the file property.
5fill in blank
hard

Fill all three blanks to create a memory storage multer setup and access the uploaded file buffer in the route.

Express
const storage = multer.[1]();
const upload = multer({ storage });

app.post('/upload', upload.single('file'), (req, res) => {
  const fileData = req.file.[2];
  console.log(fileData);
  res.send('File size: ' + req.file.[3] + ' bytes');
});
Drag options to blanks, or click blank then click option'
AmemoryStorage
Bbuffer
Csize
Doriginalname
Attempts:
3 left
💡 Hint
Common Mistakes
Using diskStorage instead of memoryStorage.
Accessing file path instead of buffer.
Using wrong property for file size.