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 Node.js Basics (2014) Building a Command Line Application Perfecting: Getting Multiple Profiles

Alex Flores
Alex Flores
7,864 Points

Should I use AJAX or Node.js for this?

For my profile site, I want to pull all of my badges and javascript points from Team Treehouse. I know I can create a node.js script that pulls the data, but it seems like the same thing that can be accomplished with AJAX and much easier.

To me, it seems like it would be more efficient to use AJAX, as the user computer only has to interact with the Team Treehouse server. Whereas Node.js would have my website server request the data and then I would have to send the data to the users computer.

Am I missing something? What are the advantages and disadvantages using each one?

4 Answers

Garrett Levine
Garrett Levine
20,305 Points

You're correct ! That code through isn't a great example of what node is often used for, but just something to get you a little bit of experience under your belt. Often times you'll make API calls from the client. The only time you'd want to make API calls from a server, node or otherwise, is if you didn't want your API key/API secret to be revealed (since anyone can sort through your front-end code and see it). In the case with retrieving badge count and bade information, you don't even need an API key! So you'll be fine creating what you need from the client-side JS!

Garrett Levine
Garrett Levine
20,305 Points

also some APIs require you to make AJAX requests from a server, and not from a client for security reasons.

Garrett Levine
Garrett Levine
20,305 Points

In the situation where you want to request data from another server/api and render it in the browser, you will want to use AJAX.

Node is a back-end server-side language that is used to manage data on a server. It is used to interact with databases. Any time you're looking to access already existing data on a site and render it in the client's browser, plain AJAX is all you'll need.

If you are interested in storing user data, and being able to retrieve it and manipulate it, that's when you'll need node and a database.

Alex Flores
Alex Flores
7,864 Points

Thanks! That is what I was thinking, but I'm wondering why the learning module "Build a simple Dynamic side with Node.js" had us using node.js to pull the user information from team treehouse? Maybe they just did this as a means to teach us, but they didn't mention anywhere that it wasn't the best way to complete this task.

This actually leads me to another question, in the learning module "Build a simple Dynamic side with Node.js" it doesn't appear that they are storing the retrieved data on a database, but rather just immediately sending it to the client's computer - is this right?

Sorry, I'm assuming you have taking this course. If not, don't worry about it. Thanks again for your help.

Steven Parker
Steven Parker
229,695 Points

:point_right: Ajax and Node.js are not an either/or decision.

Ajax is client-side method of exchanging data with a server and updating parts of a page without a total reload. Node.js is a runtime environment for server-side JavaScript. They do different jobs in different places. You could potentially use both.

It sounds like you're wondering if you should create a client (browser) application or a server (desktop) one. Who is the intended audience, and how will it be used?

Alex Flores
Alex Flores
7,864 Points

I'm just creating a profile website and in the "About Me" section I want to show all of the badges and scores for the different categories. The intended audience is for really whoever is interested in learning more about me. So, I imagine it won't get a lot of traffic - for now.

So which should I use for this and under what conditions should I use AJAX and which should I use node?

Thanks again Steven Parker!