Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Node.js Basics (2014) Introduction to Node.js The Tools at our Disposal

6,730 Points

Why would you set up a web server via Node Js? Or at all?

I keep watching node js tutorial on the fullstack track. Call me silly but I don't know why would you set up a web server in the first placE?

1 Answer

28,526 Points

Web servers are what actually listens to connections when people connect to your website.

When you connect to Treehouse for instance, your browser sends a request to the Treehouse web server, which looks at the page you requested and figures out what data to send you. Without that being present the website would not work at all.

As for why you would use node to run the web server, the main reason is that it allows you to use JavaScript to write your server side code. Normally you would have to be write that in some other language like PHP, Ruby, or Java.

Server side code is code that is executed on the server itself, instead of on the user's computer. The reason why you need server side code is that you have no way of stopping users from looking at and modifying code you send to their browser.

With client side code you have to just accept that it is possible that a user might mess with the code without your permission. That is fine in most instances when it just comes to code that makes the site easier to use or more functional, since messing with those functions doesn't really benefit a malicious user. But when it comes to code that has to be secure, like code responsible for authenticating that a user has access to a certain page or code that verifies that a form is actually valid they has to be done server side. Since you can't trust the client side code to actually run as intended.