├── .gitignore ├── public ├── main.js ├── styles.css └── index.html ├── package.json └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .env -------------------------------------------------------------------------------- /public/main.js: -------------------------------------------------------------------------------- 1 | function run() { 2 | fetch("/api/movie") 3 | .then((response) => response.json()) 4 | .then((data) => { 5 | const detailsElement = document.getElementById("movie"); 6 | 7 | detailsElement.getElementsByTagName("img")[0].src = data.poster; 8 | detailsElement.getElementsByTagName("h1")[0].innerText = data.title; 9 | detailsElement.getElementsByTagName("p")[0].innerText = data.fullplot; 10 | 11 | detailsElement.style.visibility = "visible"; 12 | }); 13 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leaflix", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node server.js", 8 | "start:dev": "nodemon -r dotenv/config server.js", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "keywords": [], 12 | "author": "", 13 | "license": "ISC", 14 | "dependencies": { 15 | "dotenv": "^8.2.0", 16 | "express": "^4.17.1", 17 | "mongodb": "^3.6.0" 18 | }, 19 | "devDependencies": { 20 | "nodemon": "^2.0.4" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /public/styles.css: -------------------------------------------------------------------------------- 1 | h1 { 2 | font-family: 'Barlow' ; 3 | } 4 | 5 | body { 6 | font-family: 'Barlow' ; 7 | width: 50%; 8 | margin-left: 25%; 9 | text-align: justify; 10 | line-height: 1.7; 11 | font-size: 18px; 12 | } 13 | 14 | button { 15 | font-size: 18px; 16 | font-weight: 600; 17 | color: #000; 18 | font-family: 'Barlow' ; 19 | } 20 | 21 | #laugh { 22 | background-color: #fedc65; 23 | } 24 | 25 | #movie { 26 | display: flex; 27 | flex-direction: row; 28 | margin-top: 45px; 29 | } 30 | 31 | #movie img { 32 | margin-right: 45px; 33 | width: 400px; 34 | height: 600px; 35 | object-fit: cover; 36 | } 37 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |

🎥 Welcome to Leaflix

11 |

What will you watch today?

12 | 13 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | // create an express app 2 | const express = require("express"); 3 | const app = express(); 4 | 5 | const { MongoClient } = require("mongodb"); 6 | 7 | const uri = "mongodb+srv://USERNAME:PASSWORD@CLUSTER_NAME.mongodb.net/DATABASE_NAME?retryWrites=true&w=majority"; 8 | 9 | // use the express-static middleware 10 | app.use(express.static("public")); 11 | 12 | // define the first route 13 | app.get("/api/movie", async function (req, res) { 14 | const client = new MongoClient(uri, { useUnifiedTopology: true }); 15 | 16 | try { 17 | await client.connect(); 18 | 19 | const database = client.db('sample_mflix'); 20 | const collection = database.collection('movies'); 21 | 22 | // Query for a movie that has the title 'Back to the Future' 23 | const query = { genres: "Comedy", poster: { $exists: true } }; 24 | const cursor = await collection.aggregate([ 25 | { $match: query }, 26 | { $sample: { size: 1 } }, 27 | { $project: 28 | { 29 | title: 1, 30 | fullplot: 1, 31 | poster: 1 32 | } 33 | } 34 | ]); 35 | 36 | const movie = await cursor.next(); 37 | 38 | return res.json(movie); 39 | } catch(err) { 40 | console.log(err); 41 | } 42 | finally { 43 | // Ensures that the client will close when you finish/error 44 | await client.close(); 45 | } 46 | }); 47 | 48 | // start the server listening for requests 49 | app.listen(process.env.PORT || 3000, 50 | () => console.log("Server is running...")); 51 | --------------------------------------------------------------------------------