An Express route, which renders a page, and makes use of socket.io
\$\begingroup\$
Nội Dung Chính
Conditional line not in brackets
There is an if
statement in queryAPI()
without brackets. This impacts readability greatly- especially if the code to be conditionally executed gets moved to the line below. It is best to always include brackets.
I haven’t tested this with your code but you could simplify the if
/else
to a statement that uses short-circuit evaluation with logical OR:
return api.data[0] || { name: [], quote: [], deathCount: [] };
Variable names
Some variables are misleading e.g.:
const api = await axios.get(`https://www.breakingbadapi.com/api/${endpoint}`);
The return value should be an API response – not an API. Thus a name like apiResponse
or response
would be more descriptive.
Constants in all capitals
For constants that shouldn’t change, most style guides for JavaScript (as well as similar languages) call for constant names to be all uppercase.
E.g.
const UPDATE_TIME = 2000;
const TOTAL_CHAR = 54;
const INITIAL_CHAR = 1;
I am presuming there was a typo in the name initalChar
.
It might also be wise to define URLs in constants.
Those variables could be moved out of the functions and put near the top of the code or in an included module. That way if any of them need to be updated you wouldn’t have to hunt through the code.
Simplify interval function
The callback can just be a reference. So anonymous function in the block:
setInterval(() => { updatePageWithRandomCharacter(); }, updateTime);
Can be shortened to:
setInterval(updatePageWithRandomCharacter, UPDATE_TIME);
That line could replace the call to runCharacterUpdateInterval()
and then that function wouldn’t be needed anymore.
Excess await
keywords
Unless I am missing something, there are more await
keywords than are necessary. For example:
return await { bio, quote, deathCount };
Should not need the await
keyword because it is just a plain object.
The same applies to this line:
if (await api.data[0]) return await api.data[0];
Useless call to Math.floor()
The return line of getRandomCharacterId()
is:
return Math.floor(Math.random() * Math.floor(totalChar)) + initalChar;
And totalChar
is assigned the value 54
which is an integer and this equivalent to the floor of 54
so that call to Math.floor()
can be removed.
Arrow functions could be a single line
It is not mandatory but simple functions don’t need brackets or a return
– for example:
const encode = name => { return name.replace(/ /g, "+"); };
Could be simplified to:
const encode = name => name.replace(/ /g, "+");
The same is true for this promise callback:
createCharacter().then(profile => { socket.push("breaking bad", profile); });
Though if the brackets were removed then the return value from the call to socket.push()
would then be returned from the callback.