Week 1: Introduction to Node.js
Windows, Mac or Linux
Visual Studio Code Node.JS (incl. NPM)Node.js runs JavaScript on a server. This differs it from normal JavaScript since it was originally designed to run inside a Web Browser.
Installing on Windows is simple. Just go to their website and download the installer. Just make sure the option's ticked to add it to your PATH.
So as long as node is added to your PATH, you should be able to do the following:
So you can code in Node.js as if you were just typing normal JavaScript.
When logging through console, it will output to the terminal you launch the script in.
console.log("Hello World");
The language is fundamentally the same as the JS you use in a browser.
Just remember that it uses a more modern *flavour* of js called ES6.
ES6 brings with it extra features for basic things like for loops. See the example below and figure out what is does:
// Old JS
var arr = ["A", "B", "C"];
for (var i in arr) {
console.log(arr[i]);
}
// ES6
let arr = ["A", "B", "C"];
for (let a of arr) {
console.log(a);
}
Node.JS can separate your code into unique 'modules'. Modules can be imported by passing their name as a string to the built in function require()
.
A module can be one of the following:
package.json
file containing a "main"
field.index.js
file in it.We will cover package.json
, NPM and Express.js