nodejs + express + dynamic room generation with url generator

This commit is contained in:
Kacper Maj 2021-01-17 20:28:43 +01:00
parent aa219ad431
commit 6f2c7df7e7
20 changed files with 2113 additions and 221 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

Binary file not shown.

View File

@ -1,52 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat&display=swap" rel="stylesheet" />
<link href="./landing-page-css.css" rel="stylesheet" />
<title>Document</title>
<script type="text/javascript" defer src="index.js"></script>
<!-- Bootstrap -->
<!-- Font Awesome -->
<link
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
rel="stylesheet"
/>
<!-- Google Fonts -->
<link
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
rel="stylesheet"
/>
<!-- MDB -->
<link
href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.0.0/mdb.min.css"
rel="stylesheet"
/>
<!-- MDB -->
<script
type="text/javascript"
defer src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.0.0/mdb.min.js"
></script>
</head>
<body>
<div class="background">
<div id="nav" >
<img src="images/wwf-text.png" alt="text" class="wwf-text">
<img src="images/logo-bez-tla.png" alt="logo" class="logo">
<button id="create-room" class="centered button-create-room">Stwórz pokój!</button>
</div>
<div id="footer" class="fixed-bottom">
<footer class="bg-grey text-center text-lg-start">
<!-- Copyright -->
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.4)">
<a class="text-light">Stworzone przez Kacpra Maja na zajęcia Pracowni Programowania</a>
<br/>
<a class="text-light">Numer indeksu: s457990</a>
</div>
<!-- Copyright -->
</div>
</div>
</body>
</html>

View File

@ -1,5 +0,0 @@
"use strict";
const button = document.getElementById("create-room");
button.addEventListener('click', () => console.log("dziala!"));

View File

@ -1,70 +0,0 @@
.wwf-text {
/* wwf-text 1 */
display: block;
/* position: fixed; */
margin-left: auto;
margin-right: auto;
margin-top: -5%;
transform: scale(0.4);
filter: drop-shadow(-11px 8px 5px rgba(0, 0, 0, 0.63))
}
.logo {
/* logo-bez-tla 1 */
display: block;
margin-left: auto;
margin-right: auto;
margin-top:-12rem;
transform: scale(0.8);
filter: drop-shadow(-11px 11px 7px rgba(0, 0, 0, 0.63));
}
.background {
/* background-langing-page 1 */
position: absolute;
width: 100%;
height: 100%;
background-position: center;
left: 0px;
top: 0px;
background-image: url(images/background-langing-page.png);
}
.button-create-room {
box-shadow:inset 0px 1px 0px 0px #cf866c;
background:linear-gradient(to bottom, #d0451b 5%, #bc3315 100%);
background-color:#d0451b;
border-radius:25px;
border:1px solid #942911;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family: Montserrat;
font-size:25px;
padding:10px 30px;
text-decoration:none;
text-shadow:0px 1px 0px #854629;
display: block;
margin-left: auto;
margin-right: auto;
}
.button-create-room:hover {
background:linear-gradient(to bottom, #bc3315 5%, #d0451b 100%);
background-color:#bc3315;
position:relative;
top:-2px;
}
.button-create-room:active {
position:relative;
top:3px;
}

1777
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "watch-with-friends",
"version": "1.0.0",
"description": "**Watch with friends** to projekt, który ma za zadanie umożliwić „spotkanie” się znajomych na wspólnym oglądaniu filmów w dobie pandemii. Postawiłem sobie za zadanie umożliwić połączenie możliwie wielu przyjaciół jednocześnie w jednym, generowanym losowo pokoju do którego dostęp otrzymują osoby, które dostaną unikatowy link. W pokoju tym użytkownicy poproszeni zostaną o ustawienie swojej nazwy a następnie mogą zarządzać puszczanym aktualnie filmem z YouTube, wkleić link do wybranego przez siebie filmu, dowolnie zatrzymywać i puszczać film. Jako dodatkowy element pokoju dodany zostanie chat, który będzie pozwalał na komunikację pomiędzy użytkownikami serwisu w czasie rzeczywistym.",
"main": "index.js",
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js --ignore src/utils/roomsIDs.json"
},
"repository": {
"type": "git",
"url": "https://git.wmi.amu.edu.pl/s457990/Watch-with-friends"
},
"author": "Kacper Maj",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"fs": "0.0.1-security",
"hbs": "^4.1.1",
"mdbootstrap": "^4.19.2",
"request": "^2.88.2"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}

View File

@ -0,0 +1,60 @@
.wwf-text {
/* wwf-text 1 */
display: block;
/* position: fixed; */
margin-left: auto;
margin-right: auto;
margin-top: -5%;
transform: scale(0.4);
filter: drop-shadow(-11px 8px 5px rgba(0, 0, 0, 0.63));
}
.logo {
/* logo-bez-tla 1 */
display: block;
margin-left: auto;
margin-right: auto;
margin-top: -12rem;
transform: scale(0.8);
filter: drop-shadow(-11px 11px 7px rgba(0, 0, 0, 0.63));
}
.background {
/* background-langing-page 1 */
position: absolute;
width: 100%;
height: 100%;
background-position: center;
left: 0px;
top: 0px;
background-image: url(../img/background-langing-page.png);
}
.button-create-room {
box-shadow: inset 0px 1px 0px 0px #cf866c;
background: linear-gradient(to bottom, #d0451b 5%, #bc3315 100%);
background-color: #d0451b;
border-radius: 25px;
border: 1px solid #942911;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: Montserrat;
font-size: 25px;
padding: 10px 30px;
text-decoration: none;
text-shadow: 0px 1px 0px #854629;
display: block;
margin-left: auto;
margin-right: auto;
}
.button-create-room:hover {
background: linear-gradient(to bottom, #bc3315 5%, #d0451b 100%);
background-color: #bc3315;
position: relative;
top: -2px;
}
.button-create-room:active {
position: relative;
top: 3px;
}

View File

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.6 MiB

View File

Before

Width:  |  Height:  |  Size: 44 KiB

After

Width:  |  Height:  |  Size: 44 KiB

View File

Before

Width:  |  Height:  |  Size: 85 KiB

After

Width:  |  Height:  |  Size: 85 KiB

30
public/js/landingPage.js Normal file
View File

@ -0,0 +1,30 @@
'use strict';
const button = document.getElementById('create-room');
const roomCreateForm = document.getElementById('create-room-form');
const ID = () => Math.random().toString(36).substr(2, 12);
const sendDataJson = function (data) {
fetch('/room', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data);
})
.catch((error) => {
// console.error('Error:', error);
});
};
button.addEventListener('click', (e) => {
e.preventDefault();
const idOfRoom = ID();
sendDataJson({ id: idOfRoom });
console.log('button dziala');
location.href = `/room/${idOfRoom}`;
});

View File

@ -1,94 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat&display=swap" rel="stylesheet" />
<link href="./room-style.css" rel="stylesheet" />
<script type="text/javascript" defer src="room.js"></script>
<title>Document</title>
<!-- Bootstrap -->
<!-- Font Awesome -->
<link
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
rel="stylesheet"
/>
<!-- Google Fonts -->
<link
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
rel="stylesheet"
/>
<!-- MDB -->
<link
href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.0.0/mdb.min.css"
rel="stylesheet"
/>
<!-- MDB -->
<script
type="text/javascript"
defer src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.0.0/mdb.min.js"
></script>
</head>
<body>
<div id="header">
<!-- navbar icon -->
<nav class="navbar navbar-light bg-dark">
<div class="container">
<a class="navbar-brand" href="#">
<img
src="images/logo-bez-tla.png"
height="120"
alt=""
loading="lazy"
/>
</a>
<!-- search form -->
<form class="d-flex input-group w-50 ">
<input type="search" class="form-control" id="yt-link" placeholder="Wklej link do filmu na youtubie" aria-label="Search"/>
<button class="btn btn-outline-light" id="link-do-filmu-btn" type="button" data-mdb-ripple-color="light">
Oglądaj!
</button>
</form>
</div>
</nav>
</div>
<!-- YT PLAYER -->
<div class="row light-dark d-flex justify-content-center">
<div class="col-md-8 d-flex justify-content-center">
<div id="player">
<iframe id="yt-player-iframe" width="900" height="500" src="https://www.youtube.com/embed/9YffrCViTVk" frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
</div>
</div>
<!-- CHAT -->
<div id="chat" class="col-md-4 d-flex justify-content-center">
dsadsa
</div>
</div>
<div id="footer" class="fixed-bottom">
<footer class="bg-grey text-center text-lg-start">
<!-- Copyright -->
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.4)">
<a class="text-light" href="https://mdbootstrap.com/">Stworzone przez Kacpra Maja na zajecia Pracowni Programowania</a>
<br/>
<a class="text-light" href="https://mdbootstrap.com/">Numer indeksu: 457990</a>
</div>
<!-- Copyright -->
</div>
</body>
</html>

55
src/index.js Normal file
View File

@ -0,0 +1,55 @@
const path = require('path');
const express = require('express');
const hbs = require('hbs');
const request = require('request');
const bodyParser = require('body-parser');
const { addToJson, checkIfInJson } = require('./logic');
require('./logic');
// Establishing the server
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.raw());
const port = process.env.PORT || 3000;
// Establishing routes to views directory
const viewsPath = path.join(__dirname, '../templates/views');
// setup handlebars, engine, views locations
app.set('view engine', 'hbs');
app.set('views', viewsPath);
// setup static dir to serve
app.use(express.static(path.join(__dirname, `../public`)));
//Establishing the routers for app.get
app.get('', (req, res) => {
res.render('index', {});
});
app.post('/room', (req, res) => {
// console.log(req.body.id);
addToJson(req.body.id);
// res.redirect('/room/:id');
});
app.get('/room/:id', (req, res) => {
// sprawdzam czy id znajduje sie w jsonie aktualnie stworzonych pokojow, jesli tak to pozwalam sie podlaczyc
// jak uzytkownik wychodzi to sprawdzam w socketIO ile ludzi podlaczonych jest aktualnie do pokoju
// console.log(req.params.id);
// console.log(checkIfInJson(req.params.id));
if (!checkIfInJson(req.params.id)) return res.send('no found');
res.render('room', {});
});
app.get('*', (req, res) => {
res.send('404', {
title: 'page not found',
pageName: '404 error',
});
});
// Deploying server
app.listen(port, () => {
console.log(`server is up on port ${port}`);
});

34
src/logic.js Normal file
View File

@ -0,0 +1,34 @@
const fs = require('fs');
// dodac funkcje usuwania i filtrowania
const loadFromJson = () => {
try {
const dataBuffer = fs.readFileSync('src/utils/roomsIDs.json');
const dataJson = dataBuffer.toString();
return JSON.parse(dataJson);
} catch (e) {
return [];
}
};
const addToJson = (id) => {
const dataLoaded = loadFromJson();
dataLoaded.push({
id,
});
const dataJSON = JSON.stringify(dataLoaded);
fs.writeFileSync('src/utils/roomsIDs.json', dataJSON);
};
const checkIfInJson = (id) => {
const data = loadFromJson();
let inJson = false;
data.forEach((roomID) => {
// console.log(roomID.id);
if (roomID.id === id) inJson = true;
});
return inJson;
};
module.exports = {
addToJson,
checkIfInJson,
};

1
src/utils/roomsIDs.json Normal file
View File

@ -0,0 +1 @@
[{"id":"devroom"},{"id":"3fonw8h8u1c"},{"id":"msvq9gmd8x"},{"id":"35yw1clt8uj"},{"id":"v34lexnxkf"},{"id":"cjkxzo4ot5"},{"id":"p53znizk5n"}]

46
templates/views/index.hbs Normal file
View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat&display=swap" rel="stylesheet" />
<link href="/css/landing-page-css.css" rel="stylesheet" />
<title>Document</title>
<script type="text/javascript" defer src="/js/landingPage.js"></script>
<!-- Bootstrap -->
<!-- Font Awesome -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" rel="stylesheet" />
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<!-- MDB -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.0.0/mdb.min.css" rel="stylesheet" />
<!-- MDB -->
<script type="text/javascript" defer
src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.0.0/mdb.min.js"></script>
</head>
<body>
<div class="background">
<div id="nav">
<img src="/img/wwf-text.png" alt="text" class="wwf-text">
<img src="/img/logo-bez-tla.png" alt="logo" class="logo">
<form method="POST" id="create-room-form">
<button type="submit" id="create-room" class="centered button-create-room">Stwórz pokój!</button>
</form>
</div>
<div id="footer" class="fixed-bottom">
<footer class="bg-grey text-center text-lg-start">
<!-- Copyright -->
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.4)">
<a class="text-light">Stworzone przez Kacpra Maja na zajęcia Pracowni Programowania</a>
<br />
<a class="text-light">Numer indeksu: s457990</a>
</div>
<!-- Copyright -->
</div>
</div>
</body>
</html>

83
templates/views/room.hbs Normal file
View File

@ -0,0 +1,83 @@
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat&display=swap" rel="stylesheet" />
<link href="/css/room-style.css" rel="stylesheet" />
<script type="text/javascript" defer src="/js/room.js"></script>
<title>Document</title>
<!-- Bootstrap -->
<!-- Font Awesome -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" rel="stylesheet" />
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<!-- MDB -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.0.0/mdb.min.css" rel="stylesheet" />
<!-- MDB -->
<script type="text/javascript" defer
src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.0.0/mdb.min.js"></script>
</head>
<body>
<div id="header">
<!-- navbar icon -->
<nav class="navbar navbar-light bg-dark">
<div class="container">
<a class="navbar-brand" href="#">
<img src="/img/logo-bez-tla.png" height="120" alt="" loading="lazy" />
</a>
<!-- search form -->
<form class="d-flex input-group w-50 ">
<input type="search" class="form-control" id="yt-link" placeholder="Wklej link do filmu na youtubie"
aria-label="Search" />
<button class="btn btn-outline-light" id="link-do-filmu-btn" type="button" data-mdb-ripple-color="light">
Oglądaj!
</button>
</form>
</div>
</nav>
</div>
<!-- YT PLAYER -->
<div class="row light-dark d-flex justify-content-center">
<div class="col-md-8 d-flex justify-content-center">
<div id="player">
<iframe id="yt-player-iframe" width="900" height="500" src="https://www.youtube.com/embed/9YffrCViTVk"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
</div>
</div>
<!-- CHAT -->
<div id="chat" class="col-md-4 d-flex justify-content-center">
dsadsa
</div>
</div>
<div id="footer" class="fixed-bottom">
<footer class="bg-grey text-center text-lg-start">
<!-- Copyright -->
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.4)">
<a class="text-light" href="https://mdbootstrap.com/">Stworzone przez Kacpra Maja na zajecia Pracowni
Programowania</a>
<br />
<a class="text-light" href="https://mdbootstrap.com/">Numer indeksu: 457990</a>
</div>
<!-- Copyright -->
</div>
</body>
</html>