Skip to content
← Back to Community
node js mega tutorial
Profile icon
AmoghTheCool

Mega Node.js tutorial

There is another way to see the tutorial, just go here for the online version (web server written with Express in node.js).

What is it?

node.js is plain JS, except it can run OUTSIDE of browsers! Let me explain. Regular JS runs through HTML, the HTML provides src links to the script, which the browser (Chrome, Firefox, MS Edge, Internet Explorer, etc...) parses and runs. However, JS cannot be run outside the browser, for example to make desktop apps, without frameworks such as Electron. node.js can be run outside the browser, inside the terminal, some of which are Linux shell (LinuxOS), PowerShell (Windows), Command Line (Windows + MacOS). The node.js compiler/interpreter is actually the V8 engine, which is an/the engine browsers use to parse JS code.

According to Bing:

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" paradigm, unifying web-application development around a single programming language, rather than different languages for server-side and client-side scripts.

Why should I use node.js?

Well, first of all, node.js is one of the most supported languages. By that, I mean there are TONS and TONS of frameworks and modules to make your life easier (much like the ecosystem Python provides with PyPi). There are frontend frameworks (like Express) and backend frameworks (like Next.js).

Also, it makes it easier to communicate between frontend JS and backend node.js since the datatype encoding is almost exactly the same.

Lastly, the syntax is great and readable and works for terminal/console apps (you can use prompt). I would recommend node.js even more than Python.

Getting Started with node.js

Developing locally:

First, you want to click here and click on your OS. Finish the setup.

Developing with preinstalled packages (using an IDE like replit.com):

Just create a new node.js Repl.

Variables

Tip: To create a comment, just type // to comment the entire line and /* and */ to do multilined (or even half-line) comments

There are three ways to declare variables in node.js and JS:

let varName = varValue; var varName = varValue; const varName = varValue;

let creates a mutable variable. Mutable means the variable can be reassigned.
const creates a constant variable, which means the variable cannot be changed. var is basically the same as let.

Note: Even if an Object is put into a variable using const, its properties can be changed.

To create a String, just do "string_here". To create an Int or Float, just do int_or_float_here. If you created the int or float right, then you should see the int being highlighted in light-green. To create a bool, just type true or false (all lowercase). To create a list, the syntax is similar to Python: ["list","items","seperated","with","commas"]. To add items to the list, just type listName.push(newItem). To INITIALIZE a dictionary, type let myDict = {}. If you want to INITIALIZE the dictionary with certain keys, then just put "key":"value" seperated with commas. If you want to create new items (or reassign items), then use square brackets like so: myDict["key"] = "value or new value".

Operators

String operators

let str1 = "foo"; let str2 = "bar"; let str3 = str1 + str2; //str3 is "foobar" let str4 = str1 * 4 + str2 * 4; // it is "foofoofoofoobarbarbarbar" /* Because of that, you can also use operators like += and *= with strings. */

Int & Float operators

let myInt = 1; let myOtherInt = 2; myInt + myOtherInt //that would be 3 myInt - myOtherInt //that would be -1 myInt * myOtherInt //that would be 2 myInt / myOtherInt //that would be 0.5 //% is the modulo operator 10 % 2 //0 9 % 2 //1 /* To change variables when operating on them, use operators like this: += changes and adds an int -= changes and subtracts an int /= changes and divides an int *= changes and multiplies an int */

Boolean operators

//! makes anything its opposite !true //false !false //true /* Funny note: you can stack up !s like this: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!true would be valid run it to see what it would be! */ //|| is the or gate true || false //true false || false //false //&& is the and gate true && true // true true && false // false //== is the equality operator 5 == 1 + 4 //true 5 == 1 + 3.999999 //false //!= is the not-equal operator 5 != 1 + 3.88888 //true 5 != 1 + 4 //false //>= is the greater than or equal operator 5 >= 4 //true 5 >= 5 //true 5 >= 6 // false //<= is the greater than or equal operator 5 <= 4 //false 5 <= 5 //true 5 <= 6 // true //> and < are comparison operators 5 > 4 //true 5 > 5 //false 5 > 6 // false

Type Casting

String to Int:

parseInt("43") //43

ANYTHING to String:

43.toString() //"43"

Boolean to Int:

+true //1 +false //0 //or Number(true) //1 Number(false) //0 //or using ternary operators (true ? 1 : 0) // 1 (false ? 1 : 0) //0

Functions

Functions are pretty important. Functions can take parameters to modify how the function behaves. Functions can also reference values using the return keyword.

function funcName(param) { return param + 'oof' } //so funcName("oof means ") //returns "oof means oof" //if i created a function without return, then it would reference void function v() { //code } v() //void

OO in node.js

IMPORTANT! This tutorial is not about Object Oriented theory so go check out some object oriented tutorials here.

Objects

To create an Object, do this:

const myObj = { propName: propValue, etc... } //Owing to the fact that functions can be passed as parameters and variables, we can have functions in objects

Or, if you want a blueprint, you can make a constructor.

function Constructor(param1, param2, param3) { return {object} } let myObject = Constructor(a, b, c)

Classes

I much prefer classes to Objects.

class ClassName { constructor (params) { this.whatever = whatever; //this refers to the current instance } //no need for function keyword fun1 {} fun2 {} }

To set/get attributes of an object, use .

Console

console.log(something) //prints out to console console.error(error) //print out text in red console.clear() //clears console prompt(prompt) //gets user input, prompting them with the param prompt

Ifs

if (bool) /*(is true)*/ { //code } else { // this executes if the bool is not true } //if the bool is true, then code will execute //if it is not true (false), then the else will execute //you can also only do an if without an else if (true) { console.log("Lol"); }

Loops

While Loops

While loops are loops that execute as long as a bool is true.

while (cond) { //code }

For loops

To create a for loop, just type something like this:

for (var iterator = 0; iterator <= loopRange; iterator+=1) { console.log(iterator); } //that will repeat loopRange + 1 times

Basically, that code inits a variable iterator. iterator automatically increments itself everytime the code inside the loop executes. The loop executes as long as the condition in the middle is true. "For loops" (one phrase) are similar to while loops, just they have an iterator.

Ternary operators

A ternary operator is hard to explain, but an example is below:

(true ? "is true" : "is false") //that will be "is true" (false ? "is true" : "duck") //that will be "duck"

This is sort of like if (bool) va1 else va2 in Kotlin.
Ternary operators are useful when, say, you are making a two-player game and you have an int turn. Everytime someone moves, turn increments. Now if turn % 2 == 0, then you want the name of the player to be,say, "Bob". Else, "Jill". Now without ternary operators it would be something like this:

let name = ""; let turn = 0; function setName() { if (turn % 2 == 0) { name = "Bob" } else { name = "Jill" } }

With ternary operators the code would be like this:

let name = ""; let turn = 0; function setName() { name = (turn % 2 == 0 ? "Bob" : "Jill") }

switch and case

Switch is a keyword that takes in a value, and inside it's body, you have possible cases of what the body is. I mean:

switch(value) { case 1: //executes if value is 1 //code break; case "foo": case "foobar": console.log("ok?") }

switch is most commonly used when processing which key is pressed in a game.
At the end of all cases, you have to put break;. However, if you want W and to do the same thing in a platformer, then you don't want to make your code redundant. Then you don't put break, and then put the code for when up is pressed in the bottom case. Like in the above example, if value is "foo", then it does the code for if value is "foobar".

[OPTIONAL] require

The require keyword in node.js is similar to importing in Python. When you are importing a package, you first need to download the package using npm. If you want to use a package that is not in npm (like made locally), you can use .\filename.js. For example, to require express, you type require("express").

Note: require returns the modules exports, so you have to store require in a var.

Exports and Imports

so you want to make your own module? or your own package? First, you have to make some code. Then, at the very bottom, you type:

module.exports = { Class1, var1, func1 }

So when someone requires or imports your module, they get the functions and classes and vars you include in module.exports.

Importing is when you get the exports from a package (usually something hosted on a cdn like jsdelivr).

import {Class1, var1, func1} from "module_name" ///now you can use Class1, var1, and func1 in your code.

A good example of exports and imports is in @LeviathanCoding's DreamforgerJS. If you look closely in the Github repo, at the end of each file, certain classes and functions are imported from other files.
In his demo (on replit), he imports some classes and functions from Dreamforger.

Voters
Profile icon
XJaso
Profile icon
AnimeBoy3
Profile icon
Dragonwood21
Profile icon
IermukhamietMam
Profile icon
Somto2007
Profile icon
DariDark666
Profile icon
zas202
Profile icon
ZacharyMyrin
Profile icon
Unknown-Faze
Profile icon
9-
Comments
hotnewtop
Profile icon
Leroy01010

THIS IS NOICE

Profile icon
AmoghTheCool

@Leroy01010 thanks!

Profile icon
zas202
Profile icon
AmoghTheCool

@zas202 what?