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 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"

1 Answer

andren
andren
28,526 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.