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 Build a Simple Dynamic Site with Node.js Creating a Simple Server in Node.js Preparing & Planning

Aakash Srivastava
Aakash Srivastava
5,415 Points

Why do we use Node.js and not AJAX?

Why are we using Node here instead of AJAX? Why do we need to create a webserver? Does it mean each website user has a webserver created for him?

It feels like Node.js basic course has left me with lots of holes, and the documentation is just plain "how" and not "why"

2 Answers

28,558 Points

Why are we using Node here instead of AJAX?

AJAX is used to make client-side code communicate with a server, it doesn't serve much use if you don't have a server to communicate with, which is where Node.js comes in. As such AJAX and Node.js are not alternatives to each other, they are in fact often used together.

Why do we need to create a webserver?

To serve your page, and to run code that is to sensitive to run on a client.

When you visit a website your web request is send to a web server (this is true for literally all websites) which processes the request and sends a page in response. That is how websites work. So you can't host a website without a web server. That doesn't mean you have to use Node.js though, there are lots of webservers available, but Node.js allows you to use JavaScript on the web server. As opposed to having to learn PHP or some other language for your server side code.

Now why would you want to write JavaScript on the server instead of just writing it as client-side code as you have done in previous lectures? Well the answer is security. Client-side code can be disabled, looked at and modified very easily by anybody visiting your website. That means that code that is critical, like code managing logins, or database connections are dangerous to include in client-side code. Code that runs on the server on the other hand cannot be modified or seen by a client.

Does it mean each website user has a webserver created for him?

No, all users are directed at the same webserver instance, at least by default. On larger sites it is possible to have multiple servers setup to deal with a large amount of requests.

and why is the tutor not explaining all these before moving on to code? the previous videos explain all these things first for us to know the essence of creating this server.