JavaScript Node.js Basics Handling Errors in Node Organizing Your Code with require

Nico Trivelli
Nico Trivelli
20,351 Points

Is there a way to avoid countless HTTP requests when using many files that require each other?

Hi community,

I understand that making your code more modular makes it more easily maintainable, and more organized in general. And I like that.

However, my (absolute inexpert) questions are:

  1. since (I think?) that generates countless HTTP requests, doesn't that affect performance quite a lot?

  2. Is there a way to avoid them, kind of like minifying but putting everything in one file (or would that even help?), or smth alike? Or is it just a compromise we must make (and permanently assess) between giving the best performance possible and making the code as maintainable and organized as possible.

Thanks for your insight, corrections, advice, counsels, ideas, etc. on this!

1 Answer

Ari Misha
Ari Misha
19,198 Points

Hiya there! Lets consider an example of a telephone call. Nowadays , you can do like conference calls with multiple friends/people but like 10 or 15 years ago , it wasnt possible actually, you'd call a friend, hangup, and then call someone else again. Thats how it used to work, right?

Now the above example explains your first question and second question too. How? The real world applications that are implemented in Nodejs, creates an open servers called cable, where client sends in multiple requests to the server and server responding back asynchronously. While server responding to one request, it can take more requests and it'll happily responding back to multiple clients . This is achieved with Sockets. And this whole process is kinda like conference call in above example, right? Traditionally, server would only server one request-response cycle and then it was destroyed after it from the pool.

Modularizing apps is more for human sets of eyes than machine. All files get compressed and cached by the client behind the scenes anyway. But organization is so important that it makes your code a lot cleaner and organized and efficient and sexy that it enhances the performance of your applications in turn. One more thing i'd like to mention, components/modules serve, is that the client doesnt have to reload the whole page , it just sends out the request for that particular module and it gets served to you asynchronously without reloading the whole page everytime. Its powerful like that.

~ Ari

Nico Trivelli
Nico Trivelli
20,351 Points

Great stuff! Thank you for the crystal-clear explanation, Ari Misha !