Skip to content

TodayILearned - 2023-04-28

Posted on:April 28, 2023 at 03:54 AM

#TIL

Multer: middleware for handling file uploads

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})

Another reminder about MongoDB:

export const postUpload = async (req, res) => {
  const { path: fileUrl } = req.file;
  const { title, description, tags } = req.body;
  try {
    await Video.create({
      title,
      description,
      hashtags: tags,
      fileUrl,
    });
  return res.redirect(`/`);
import mongoose from 'mongoose';

const videoSchema = new mongoose.Schema({
  title: { type: String, required: true, trim: true, maxLength: 80 },
  description: { type: String, trim: true },
  tags: [{ type: String }],
});
import mongoose from 'mongoose';

const videoSchema = new mongoose.Schema({
  title: { type: String, required: true, trim: true, maxLength: 80 },
  description: { type: String, trim: true },
  tags: [{ type: String }],
  fileUrl: { type: String, required: true }
});