[クローンコード]YouTube 8日目


💕 進捗


[スタック]オイルチューブクローンコード


3.5~3.12時間


😁 Multer

  • Multierは、フォームpostでファイル処理を簡略化するライブラリです.
  • を利用すると、ファイルのurlや情報を簡単に知ることができます.
  • 🤣 その他のコード

    // videoController.js
    
    import routes from "../routes";
    import Video from "../models/Video";
    
    export const home = async (req, res) => {
      try {
        const videos = await Video.find({}).sort({
          '_id': -1
        });
        res.render("home", {
          pageTitle: "Home",
          videos,
        }); // 이건 프레임워크 규약
      } catch (error) {
        console.log(error);
        res.render("home", {
          pageTitle: "Home",
          videos: [],
        }); // 이건 프레임워크 규약
      }
    };
    export const search = async (req, res) => {
      const {
        query: {
          term: searchingBy
        },
      } = req;
      let videos = []
      try {
        videos = await Video.find({
          title: {
            $regex: searchingBy,
            $options: "i"
          }
        })
      } catch (error) {
        console.log(error)
      }
      res.render("search", {
        pageTitle: "Search",
        searchingBy,
        videos
      });
    };
    export const getUpload = (req, res) =>
      res.render("upload", {
        pageTitle: "Upload",
      });
    
    export const postUpload = async (req, res) => {
      const {
        body: {
          title,
          description
        },
        file: {
          path
        },
      } = req;
      // To do: upload and save video
      const newVideo = await Video.create({
        fileUrl: path,
        title,
        description,
      });
      res.redirect(routes.videoDetail(newVideo.id));
    };
    
    export const videoDetail = async (req, res) => {
      const {
        params: {
          id
        },
      } = req;
      try {
        const video = await Video.findById(id);
        res.render("videoDetail", {
          pageTitle: video.title,
          video,
        });
      } catch (error) {
        console.log(error);
        res.redirect(routes.home);
      }
    };
    
    export const getEditVideo = async (req, res) => {
      const {
        params: {
          id
        },
      } = req;
      try {
        const video = await Video.findById(id);
        res.render("editVideo", {
          pageTitle: `Edit ${video.title}`,
          video,
        });
      } catch (error) {
        console.log(error);
        res.redirect(routes.home);
      }
    };
    
    export const postEditVideo = async (req, res) => {
      const {
        params: {
          id
        },
        body: {
          title,
          description
        },
      } = req;
      try {
        await Video.findOneAndUpdate({
          _id: id,
        }, {
          title,
          description,
        });
        res.redirect(routes.videoDetail(id));
      } catch (error) {
        console.log(error);
        res.redirect(routes.home);
      }
    };
    
    export const deleteVideo = async (req, res) => {
      const {
        params: {
          id
        }
      } = req
      try {
        await Video.findOneAndDelete({
          _id: id
        })
      } catch (error) {
        console.log(error)
      }
      res.redirect(routes.home)
    }