Disclaimer: As Stage 0 is to clone the repository and Stage 1 is to uncomment code, they have not been included in the table of contents, so the explanation starts in Stage 2.
For stage 2, my first step was to convert the port and the hostname to constants so that the code was more readable:
const PORT = 4221;
const HOSTNAME = "localhost";
Then, I checked what the requirements were:
<aside> 💡 Respond with 200
</aside>
Checked for which event gets fired when there is an income request to the server, its the socket.on("data") event.
socket.on("data", (data) => {
//code
});
Then, I simply did a socket.write() with the 200 status (following the standard guidelines for HTTP Response). Commit
socket.write("HTTP/1.1 200 OK\\r\\n\\r\\n");
On stage 3 started again with some refactoring, now we have two possible responses, so the first thing was to define them, I also added the HTTP Version as a constant so it was more clear what the code did:
const HTTP_VERSION = "HTTP/1.1";
const STATUS = {
"200": "200 OK",
"404": "404 Not Found"
}
Now that everything was a bit clearer, I started with the task: