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! While you're at it, check out some resources Treehouse students have shared here.

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

Sarah Breidenbach
Sarah Breidenbach
4,490 Points

Why use node.js?

I'm curious why you would use node.js instead of a browser. Is it just a preference? Is there some advantage?

2 Answers

andren
andren
28,558 Points

The point of Node.js is that it allows you to run JavaScript code on desktops and servers. This means that you can take your JavaScript knowledge and build desktop apps or server side code.

Based on your question I'm getting the impression you don't really know why you would want to write code on a server in the first place, as opposed to just sending the code to the browser to deal with. The answer to that question is security.

You don't have any control over what a browser does to your code. And users of your website has full access to your code, to both analyze and modify to their content. So if you are writing code that connects to a database, tries to verify that a user has entered correct login info to access a certain page, or anything else where your code could be sensitive or important then you need to do it server side.

Code that is ran on the server is never visible or modifiable by the client, they are only sent the result of that code, which is why it is far more secure. Client side JavaScript should only be used for convenience features. Things that make your site more pleasant to use or function better. It should never be used for anything that would cause security issues if it was disabled or bypassed.

This is why if you have a form on your website it's common to implement error checking both on the client side and on the server side. The client side error checking saves the user time as they get an instant error message without having to call out to the server, but bypassing that error checking is easy. Which is why the server side error checking is mandatory if you want your site to be secure.

Sarah Breidenbach
Sarah Breidenbach
4,490 Points

Thank you Andren. I plan to do front-end design/development and honestly I don't know anything about back-end/server-side. I'm learning JavaScript now. Is this something I'll learn about when I get to AJAX?

andren
andren
28,558 Points

AJAX is a technique that allows you to send and receive data from a server while your page is loaded. It requires you to either have a server of your own to communicate with or a third party server that you are getting data from.

If my memory serve me right the AJAX course on Treehouse mainly focuses on writing frond-end JavaScript code to do AJAX calls. Not on creating back-ends that can respond to those calls.

Treehouse does have a number of Node.js courses though which mainly center around server-coding. It's worth noting that when it comes to server coding you are not really limited to JavaScript, pretty much all popular languages has some framework that allows them to be used for web development. So you can use PHP, Python, Java, C# and so on.

Even if your main goal is to be a front-end developer I would recommend gaining at least a tiny bit of knowledge about how back-end programming works, as it is extremely helpful if you want to setup a site that does something to a database, and has forms or anything like that.

And it is something that you will likely be expected to at least has some basic understanding of if you intend to be an independent web developer. And even If you intend to be hired by some existing company specifically to work on the front-end of existing sites it can still be helpful to have some understanding of what is going on behind the scenes. As your code will likely end up doing a lot of calls to various servers. That is decently common on modern webpages.

Antti Lylander
Antti Lylander
9,686 Points

With node.js you can run javascript on the server side. I mean, you can use javascript at back-end as well as front.