From b119ced98d342711d7ca4943b66d547043796d39 Mon Sep 17 00:00:00 2001 From: Anatoly Kopyl Date: Mon, 28 Sep 2020 18:57:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8=D1=8E=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=B4=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавил код ошибки 404 при подключении к несуществующей комнате --- src/routes/index.svelte | 14 ++++++++++++-- src/routes/room.svelte | 26 +++++++++++++++++--------- src/server.js | 8 ++++++-- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/routes/index.svelte b/src/routes/index.svelte index 4080a3e..0701788 100644 --- a/src/routes/index.svelte +++ b/src/routes/index.svelte @@ -9,10 +9,15 @@ } let id; + let invalidId = false; function connect() { - const url=`${process.env.URL}/room/?id=${id}`; - window.location.href = url; + if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(id)) { + const url=`${process.env.URL}/room/?id=${id}`; + window.location.href = url; + } else { + invalidId = true; + } } const plcType = ['Лабораторная работа', 'Лаба', 'Практическая работа', 'Практика', 'Лабораторная', 'Доклад']; @@ -60,6 +65,7 @@
Код подключения:
+ {#if invalidId}Неверный формат кода :({/if}
@@ -83,6 +89,10 @@ width: 100%; } + .error-hint { + color: indianred; + } + .btn { width: 15ch; } diff --git a/src/routes/room.svelte b/src/routes/room.svelte index 172abb5..218a254 100644 --- a/src/routes/room.svelte +++ b/src/routes/room.svelte @@ -10,9 +10,10 @@ import { onMount } from 'svelte'; let room; + let res; async function getRoom() { var url = `${process.env.URL}/api/join/?id=${parsed.id}`; - const res = await fetch(url); + res = await fetch(url); room = await res.json(); } @@ -106,8 +107,14 @@ Код подключения: {parsed.id} - {:else} - Loading... + {:else if res==undefined} +
Загрузка...
+ {:else if res.status==404} +
+

404

+ Такой комнаты не существует :(
+ Вернуться на главную +
{/if} @@ -198,15 +205,16 @@ display: inline-block; } - /* - .individual:before { - background-color: #2196F3; + .status { + width: 100%; + text-align: center; + margin-top: 250px; } - .group:before { - background-color: #f57323; + .status > h1 { + font-size: 128px; + font-weight: bold; } - */ @media only screen and (max-width: 1024px) { ol { diff --git a/src/server.js b/src/server.js index d2b5a9a..5ed1ae2 100644 --- a/src/server.js +++ b/src/server.js @@ -53,8 +53,12 @@ MongoClient.connect(process.env.DB_CONNECTION, { useUnifiedTopology: true }) // id app.get('/api/join/', (req, res) => { rooms.findOne({ "id": req.query.id }).then(room => { - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.write(JSON.stringify(room)); + if (room !== null) { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.write(JSON.stringify(room)); + } else { + res.writeHead(404, { 'Content-Type': 'application/json' }); + } res.end(); }); });