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 trialAbdullah Howard
11,955 PointsHow does Node.js work?
Hey guys. I just recently finished the node.js basics which was pretty simple enough. I'm am just confused on how it works. When I install node to my visual studio code does it convert the entire script to a node script? I noticed when I type in "document" within that script it says "not defined" because the DOM doesn't exist in node. So my questions are:
When I activate node will the entire script be a node script? How does this work when I'm using a hosting service? Do I just make a new file to write client side js?
1 Answer
Brandon White
Full Stack JavaScript Techdegree Graduate 34,662 PointsHi Abdullah,
Node is an environment that allows you to write javascript that can be run outside of a browser. That’s the simplest way I think I can explain it.
There isn’t such a thing as a Node script, really. It’s just plain JavaScript, but because the DOM is a browser feature, the Node environment has no idea what “document” is, as document is not a keyword in Node. You seem to understand that part of it though.
You can read the DOCs at: https://nodejs.org/docs/latest-v13.x/api/ to find out what’s available for you to access and manipulate within the Node environment. Outside of writing your own servers, one of the things that I found cool about Node was its fs (file system) module, which allowed me to both read csv files from my computer as well as write csv files to it.
There’s tons you can do with Node. You’re gonna love it the more you work with it.
As far as hosting goes, companies like Netlify and Heroku build the apps based on your repo on sites like Github. But if you’re using a different hosting platform I imagine that the file extension clues the hosting service on which environment/interpreter can be used to read/execute your files. For example, a script that has an extension of py signifies that the code inside that file is written in python, and a script that has an extension of js signifies that the code inside that file is written in JavaScript (or for the Node environment).
Abdullah Howard
11,955 PointsAbdullah Howard
11,955 PointsHey Brandon, thanks for the response.
The thing that is confusing me is this "node environment". When I install node on visual studio, I install it in the console. But when I try to access the DOM in my js file it says "DOM is undefined". So my question is does the whole script become the node environment, or is it just the command line in the console? If it is just the command line in the console then how would that work on an actual hosting site. How would I install node there? And if the entire script becomes the environment then do I have to create another script to access the dom?
Brandon White
Full Stack JavaScript Techdegree Graduate 34,662 PointsBrandon White
Full Stack JavaScript Techdegree Graduate 34,662 PointsOkay Abdullah,
A few things, just to get us on the same page.
node
at the terminal prompt runs the Node application. That's the node environment, and it's essentially the same style of environment as the console in the developer tools section of a browser. So you can do arithmetic, create variables, etc. It's like a REPL (read evaluate print loop), if you happen to be familiar with the term.node <name of script>
, and node will attempt to interpret the script.node server.js
in your terminal, and Node would execute the file. The code inside server.js would essentially tell your computer to listen for any requests at a certain address on your computer (typically 127.0.0.1, also known as localhost). And when a request is made to that address (and the correct port), your server (the server.js file which is still being run via Node) would send back the pages you've instructed to be sent back.node server.js
which causes your program to run. So you don't install Node on the hosting service's site. That's abstracted from you. They will already have the correct version of Node already installed.