Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion JavaScript/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class Client {

static async getInstance(req, res) {
const client = new Client(req, res);
await Session.restore(client);
try {
await Session.restore(client);
} catch {
throw new Error("Couldn't restore session");
}
return client;
}

Expand Down
10 changes: 9 additions & 1 deletion JavaScript/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const http = require('node:http');
const Client = require('./client.js');
const Session = require('./session.js');

const INVALID_TOKEN_CODE = 498;

const routing = {
'/': async () => '<h1>welcome to homepage</h1><hr>',
'/start': async (client) => {
Expand Down Expand Up @@ -46,7 +48,13 @@ const types = {
};

http.createServer(async (req, res) => {
const client = await Client.getInstance(req, res);
try {
const client = await Client.getInstance(req, res);
} catch {
res.statusCode = INVALID_TOKEN_CODE;
res.end();
return;
}
const { method, url, headers } = req;
console.log(`${method} ${url} ${headers.cookie}`);
const handler = routing[url];
Expand Down
5 changes: 4 additions & 1 deletion JavaScript/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class Session extends Map {
if (sessionToken) {
return new Promise((resolve, reject) => {
storage.get(sessionToken, (err, session) => {
if (err) reject(new Error('No session'));
if (err) {
reject(new Error('No session'));
return;
}
Object.setPrototypeOf(session, Session.prototype);
client.token = sessionToken;
client.session = session;
Expand Down