Skip to content

Commit ff03fab

Browse files
committed
Refactor request handling to use parsed URL parameters and streamline game-related endpoints
1 parent d9c3f26 commit ff03fab

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

src/jsonResponses.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const notFound = (request, response) => {
1414
sendResponse(response, 404, { message: "The endpoint you are looking for was not found", id: 'notFound' }, request.method);
1515
}
1616

17-
const getGames = (request, response, parsedUrl) => {
18-
const params = parsedUrl.searchParams;
17+
const getGames = (request, response) => {
18+
const params = request.query;
1919

2020
let games = store.getAll().map(({
2121
id, name, slug, url, cover, genres, platforms, rating, rating_count, first_release_date
@@ -25,36 +25,34 @@ const getGames = (request, response, parsedUrl) => {
2525
sendResponse(response, 200, { games }, request.method);
2626
}
2727

28-
const createGame = (request, response, parsedUrl) => {
29-
const params = Object.fromEntries(parsedUrl.searchParams);
30-
sendResponse(response, 404, { method: request.method, path: parsedUrl.pathname, contentType: request.headers['content-type'], message: 'Work In Progress: createGame', params }, request.method);
28+
const createGame = (request, response) => {
29+
const params = request.query;
30+
sendResponse(response, 404, { method: request.method, path: response.path, contentType: request.headers['content-type'], message: 'Work In Progress: createGame', params }, request.method);
3131
}
3232

3333

34-
const getGame = (request, response, parsedUrl) => {
35-
const parts = parsedUrl.pathname.split('/');
36-
const idOrSlug = parts[3];
34+
const getGame = (request, response) => {
35+
const { idOrSlug } = response.params;
3736
if (!idOrSlug) return sendResponse(response, 400, { message: 'invalid/missing params.', id: 'invalidParams' }, request.method);
3837
const result = isNaN(idOrSlug) ? store.getBySlug(idOrSlug) : store.getById(Number(idOrSlug));
3938
if (!result) return sendResponse(response, 404, { message: 'Requested resource was not found.', id: 'notFound', params: idOrSlug }, request.method);
4039
return sendResponse(response, 200, result, request.method);
4140

4241
}
4342

44-
const getGenres = (request, response, parsedUrl) => {
45-
const params = Object.fromEntries(parsedUrl.searchParams);
46-
sendResponse(response, 404, { method: request.method, path: parsedUrl.pathname, message: 'Work In Progress: getGenres', params }, request.method);
43+
const getGenres = (request, response) => {
44+
const params = request.query;
45+
sendResponse(response, 404, { method: request.method, path: request.path, message: 'Work In Progress: getGenres', params }, request.method);
4746
}
4847

49-
const getPlatforms = (request, response, parsedUrl) => {
50-
const params = Object.fromEntries(parsedUrl.searchParams);
51-
sendResponse(response, 404, { method: request.method, path: parsedUrl.pathname, message: 'Work In Progress: getPlatforms', params }, request.method);
48+
const getPlatforms = (request, response) => {
49+
const params = request.query;
50+
sendResponse(response, 404, { method: request.method, path: request.path, message: 'Work In Progress: getPlatforms', params }, request.method);
5251
}
5352

54-
const updateGame = (request, response, parsedUrl) => {
55-
const parts = parsedUrl.pathname.split('/');
56-
const idOrSlug = parts[3];
57-
sendResponse(response, 404, { method: request.method, path: parsedUrl.pathname, contentType: request.headers['content-type'], message: 'Work In Progress: updateGame', idOrSlug }, request.method);
53+
const updateGame = (request, response) => {
54+
const { idOrSlug } = response.params;
55+
sendResponse(response, 404, { method: request.method, path: request.path, contentType: request.headers['content-type'], message: 'Work In Progress: updateGame', idOrSlug }, request.method);
5856
}
5957

6058
module.exports = { notFound, getGames, createGame, getGame, getGenres, getPlatforms, updateGame };

src/server.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const port = process.env.PORT || process.env.NODE_PORT || 3000;
33
const htmlHandle = require('./htmlResponses');
44
const jsonHandle = require('./jsonResponses');
55
const { styleText } = require('node:util');
6+
const { get } = require('node:http');
67
const URL_STRUCT = {
78
'/': { GET: htmlHandle.GetIndex },
89
'/docs': { GET: htmlHandle.GetDocs },
@@ -19,23 +20,29 @@ const onRequest = (request, response) => {
1920
const protocol = request.connection.encrypted ? 'https' : 'http';
2021
const parsedURL = new URL(request.url, `${protocol}://${request.headers.host}`);
2122

23+
request.query = Object.fromEntries(parsedURL.searchParams);
24+
request.path = parsedURL.pathname;
25+
26+
2227
// TODO: Remove these 2 lines when project complete
2328
const consoleColor = (method === 'GET' || method === 'HEAD') ? 'green' : 'yellow';
2429
console.log(`${styleText(consoleColor, method)} ${request.url}`);
2530

2631
let methodMap = URL_STRUCT[parsedURL.pathname];
2732
// No exact match means either 404 or dynamic endpoint ':idOrSlug'
2833
if (!methodMap) {
29-
const parts = parsedURL.pathname.split('/');
30-
if (parts.length === 4 && parts[1] === 'api' && parts[2] === 'games' && parts[3]) {
31-
methodMap = { GET: jsonHandle.getGame, POST: jsonHandle.updateGame };
34+
// This regex is AI generated MUST BE TAGGED IN THE DOCS!!!!!!!!
35+
const regexMatch = /^\/api\/games\/(?<idOrSlug>[^/]+)$/.exec(parsedURL.pathname);
36+
if(regexMatch)
37+
{
38+
response.params = {idOrSlug:regexMatch.groups.idOrSlug};
39+
methodMap = {GET: jsonHandle.getGame, POST: jsonHandle.updateGame};
3240
}
33-
else {
41+
else{
3442
jsonHandle.notFound(request, response);
3543
return;
3644
}
3745
}
38-
3946
const handler = methodMap[method] || jsonHandle.notFound;
4047
handler(request, response, parsedURL);
4148
}

0 commit comments

Comments
 (0)