Closed maanlamp closed 6 years ago
Sweet!
repository
in package.json
, use "repository": "maanlamp/listing"
, it works when linking to githubscripts
in package.json
if you’re not using themauthor
private: true
in package.json to make sure you can’t publish a project accidentallyserver-response
, but I suggest placing that in the root of your project instead of in node_modules
res.writeHead
, instead use functions to set the status code, headers, and body directlygetFileStats
, I suggest returning null
for unfound things: false
is a boolean, and suggest a value could also be true
, but null
is specifically an unfound value!utf8
in .toString("utf8")
needed?ahh I pressend send too early, but anyway, here it is!
tip: don’t use custom fonts, especially in a small 404 page, they take a while to load
Yep, I could've downloaded it but I was too lazy 😋
Don’t include your node modules, except for your own server-response, but I suggest placing that in the root of your project instead of in node_modules
Hah, whoops, forgot to .gitignore those 😅
you should look into using a javascript code-style linter!
Because I'm all over the place? Or because my style sucks? hahaha
what’s this whole bit doing?
const alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
const groupedFiles = {};
for (const letter of alphabet) {
groupedFiles[letter] = (files.filter(file => {
return file[0].toLowerCase() === letter;
}));
}
I wanted to do some fancy styling and sorting of the list of files. Because fs.readdir just gives an array of strings back, I couldn't sort the results by letter (at least not without some js). So to start I declare the alphabet as an array. Then, for every letter in that array, I set the value of te key to a filtered file array I got from fs.readdir. So I have the object groupedFiles, which gets a key for every letter of the alphabet, and every key contains an array with only strings that begin with the same letter as the key, i.e.:
{a: [adobe, amd], b: [], c: [chrome]}
I hope that made sense because I don't even understand my own explanation. Anyway, I needed that to make those fancy lists sorted and filtered alphabetically.
In the getFileStats, I suggest returning null for unfound things: false is a boolean, and suggest a value could also be true, but null is specifically an unfound value!
Yep, sloppy code. I just wanted to get it overwith at that point 😅 Hadn't thought about it.
Is the utf8 in .toString("utf8") needed?
I can't fully remember, but I think it was. I seem to recall the function returning a buffer when calling the toString method. Really weird...
Oh an finally, seems the count is always 26!
Ah right, without looking at my code I can tell why that's happening I think. I just send the amount of keys of the groupedFiles object to the filder.html template, but it always has 26 keys (1 for every letter of the alphabet). I probably should .reduce() it and count only keys with a non-emtpy array value.
Static file server
I think I did way too much, if there is such a thing 😄.
I did really like this assignment, albeit very difficult. Or perhaps i made it difficult... Oh well. In some cases it crashes and burns very violently because I generally have no idea what I'm doing, and also because urls are the worst 🤷♂️.
Other than that, I think it works perfectly.
🔗 Check out the repo