diff --git a/ssr/apis/movie.js b/ssr/apis/movie.js new file mode 100644 index 0000000..44b66f8 --- /dev/null +++ b/ssr/apis/movie.js @@ -0,0 +1,24 @@ +import { + FETCH_OPTIONS, + TMDB_MOVIE_DETAIL_URL, + TMDB_MOVIE_LISTS, +} from "../constants/api.js"; + +export const fetchMovies = async (movieType) => { + const fetchEndPoint = TMDB_MOVIE_LISTS[movieType]; + + const response = await fetch(fetchEndPoint, FETCH_OPTIONS); + const allMovieData = await response.json(); + + return allMovieData.results; +}; + +export const fetchMovieDetail = async (movieId) => { + const url = `${TMDB_MOVIE_DETAIL_URL}${movieId}`; + const params = new URLSearchParams({ + language: "ko-KR", + }); + const response = await fetch(url + "?" + params, FETCH_OPTIONS); + + return await response.json(); +}; diff --git a/ssr/constants/api.js b/ssr/constants/api.js new file mode 100644 index 0000000..dcd1f5e --- /dev/null +++ b/ssr/constants/api.js @@ -0,0 +1,22 @@ +export const BASE_URL = "https://api.themoviedb.org/3/movie"; + +export const TMDB_THUMBNAIL_URL = + "https://media.themoviedb.org/t/p/w440_and_h660_face"; +export const TMDB_ORIGINAL_URL = "https://image.tmdb.org/t/p/original"; +export const TMDB_BANNER_URL = + "https://image.tmdb.org/t/p/w1920_and_h800_multi_faces"; +export const TMDB_MOVIE_LISTS = { + nowPlaying: BASE_URL + "/now_playing?language=ko-KR&page=1", + popular: BASE_URL + "/popular?language=ko-KR&page=1", + topRated: BASE_URL + "/top_rated?language=ko-KR&page=1", + upcoming: BASE_URL + "/upcoming?language=ko-KR&page=1", +}; +export const TMDB_MOVIE_DETAIL_URL = "https://api.themoviedb.org/3/movie/"; + +export const FETCH_OPTIONS = { + method: "GET", + headers: { + accept: "application/json", + Authorization: "Bearer " + process.env.TMDB_TOKEN, + }, +}; diff --git a/ssr/constants/path.js b/ssr/constants/path.js new file mode 100644 index 0000000..d0bf975 --- /dev/null +++ b/ssr/constants/path.js @@ -0,0 +1,24 @@ +export const MOVIE_PAGE_PATH = { + home: "/", + nowPlaying: "/now-playing", + popular: "/popular", + topRated: "/top-rated", + upcoming: "/upcoming", + detail: "/detail/:movieId", +}; + +const switchMoviePaths = (originalPaths, rootPath) => { + const switchedPaths = {}; + + Object.entries(originalPaths).forEach(([key, value]) => { + if (value === "/") { + switchedPaths[value] = rootPath; + } else { + switchedPaths[value] = key; + } + }); + + return switchedPaths; +}; + +export const MOVIE_TYPE_KEY = switchMoviePaths(MOVIE_PAGE_PATH, "nowPlaying"); diff --git a/ssr/constants/placeholder.js b/ssr/constants/placeholder.js new file mode 100644 index 0000000..7b9198c --- /dev/null +++ b/ssr/constants/placeholder.js @@ -0,0 +1,9 @@ +export const HTML_PLACEHOLDERS = { + movieList: "", + movieTabs: "", + movieDetailModal: "", + bestMovieDetailTrigger: "", + bestMovieRate: "${bestMovie.rate}", + bestMovieTitle: "${bestMovie.title}", + bestMovieBackgroundImage: "${background-container}", +}; diff --git a/ssr/constants/tabs.js b/ssr/constants/tabs.js new file mode 100644 index 0000000..4369b37 --- /dev/null +++ b/ssr/constants/tabs.js @@ -0,0 +1,18 @@ +export const MOVIE_TABS = [ + { + href: "/now-playing", + label: "상영 중", + }, + { + href: "/popular", + label: "인기순", + }, + { + href: "/top-rated", + label: "평점순", + }, + { + href: "/upcoming", + label: "상영 에정", + }, +]; diff --git a/ssr/server/handler/movie-detail-modal.js b/ssr/server/handler/movie-detail-modal.js new file mode 100644 index 0000000..bdf724e --- /dev/null +++ b/ssr/server/handler/movie-detail-modal.js @@ -0,0 +1,19 @@ +import { renderMovieDetailModal } from "../render/movie-detail-modal.js"; +import { renderMoviePageHTML } from "../render/movie-page.js"; + +import { fetchMovieDetail, fetchMovies } from "../../apis/movie.js"; + +export const fetchAndRenderMovieDetailModal = async (req, res) => { + const { movieId } = req.params; + + const movieList = await fetchMovies("nowPlaying"); + const movieDetail = await fetchMovieDetail(movieId); + const moviePageTemplate = renderMoviePageHTML( + req.path, + movieList, + movieList[0] + ); + const renderedHTML = renderMovieDetailModal(moviePageTemplate, movieDetail); + + res.send(renderedHTML); +}; diff --git a/ssr/server/handler/movie-page.js b/ssr/server/handler/movie-page.js new file mode 100644 index 0000000..30eab89 --- /dev/null +++ b/ssr/server/handler/movie-page.js @@ -0,0 +1,13 @@ +import { renderMoviePageHTML } from "../render/movie-page.js"; + +import { fetchMovies } from "../../apis/movie.js"; +import { MOVIE_TYPE_KEY } from "../../constants/path.js"; + +export const fetchAndRenderMoviePage = async (req, res) => { + const movieListType = MOVIE_TYPE_KEY[req.path]; + + const movieList = await fetchMovies(movieListType); + const renderedHTML = renderMoviePageHTML(req.path, movieList, movieList[0]); + + res.send(renderedHTML); +}; diff --git a/ssr/server/index.js b/ssr/server/index.js index 09efd9a..5948d60 100644 --- a/ssr/server/index.js +++ b/ssr/server/index.js @@ -4,7 +4,9 @@ import path from "path"; import { fileURLToPath } from "url"; import movieRouter from "./routes/index.js"; + // import membersRouter from "./routes/members.js"; // 본 미션 참고를 위한 코드이며 사전 미션에서는 사용하지 않습니다. +// app.use("/members", membersRouter); // 본 미션 참고를 위한 코드이며 사전 미션에서는 사용하지 않습니다. 뉑~~~ const app = express(); const PORT = 3000; @@ -13,9 +15,7 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); app.use("/assets", express.static(path.join(__dirname, "../public"))); - app.use("/", movieRouter); -// app.use("/members", membersRouter); // 본 미션 참고를 위한 코드이며 사전 미션에서는 사용하지 않습니다. // Start server app.listen(PORT, () => { diff --git a/ssr/server/render/movie-detail-modal.js b/ssr/server/render/movie-detail-modal.js new file mode 100644 index 0000000..da3cffe --- /dev/null +++ b/ssr/server/render/movie-detail-modal.js @@ -0,0 +1,56 @@ +import { HTML_PLACEHOLDERS } from "../../constants/placeholder.js"; +import { TMDB_ORIGINAL_URL } from "../../constants/api.js"; +import { MOVIE_PAGE_PATH } from "../../constants/path.js"; + +export const renderMovieDetailModal = (moviePageTemplate, movieDetail) => { + const movieDetailHTML = getMovieDetailModalHTML(movieDetail); + + return moviePageTemplate.replace( + HTML_PLACEHOLDERS.movieDetailModal, + movieDetailHTML + ); +}; + +const getMovieDetailModalHTML = (movieDetail) => { + const { poster_path, title, genres, overview, vote_average, description } = + movieDetail; + + const genresNames = genres.map((g) => g.name); + const posterImageUrl = `${TMDB_ORIGINAL_URL}${poster_path}`; + const roundedRate = vote_average.toFixed(1); + + return /*html */ ` +
2024 · ${genresNames.join(", ")}
++ ${roundedRate} +
++ ${overview} +
+${roundedRate}
+ ${title} +들어갈 본문 작성
"; +const router = Router(); - const template = fs.readFileSync(templatePath, "utf-8"); - const renderedHTML = template.replace("", moviesHTML); +router.get(MOVIE_PAGE_PATH.home, fetchAndRenderMoviePage); +router.get(MOVIE_PAGE_PATH.nowPlaying, fetchAndRenderMoviePage); +router.get(MOVIE_PAGE_PATH.popular, fetchAndRenderMoviePage); +router.get(MOVIE_PAGE_PATH.topRated, fetchAndRenderMoviePage); +router.get(MOVIE_PAGE_PATH.upcoming, fetchAndRenderMoviePage); - res.send(renderedHTML); -}); +router.get(MOVIE_PAGE_PATH.detail, fetchAndRenderMovieDetailModal); export default router; diff --git a/ssr/views/index.html b/ssr/views/index.html index a052396..4e12f01 100644 --- a/ssr/views/index.html +++ b/ssr/views/index.html @@ -14,51 +14,29 @@