Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

nico dev
nico dev
20,364 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,323 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 dev
nico dev
20,364 Points

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