Learn to Code via Tutorials on Repl.it!

← Back to all posts
node js mega tutorial
AmoghTheCool (63)

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

Int & Float operators

Boolean operators

Type Casting

String to Int:

ANYTHING to String:

Boolean to Int:

Functions

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

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:

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

Classes

I much prefer classes to Objects.

To set/get attributes of an object, use .

Console

Ifs

Loops

While Loops

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

For loops

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

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:

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:

With ternary operators the code would be like this:

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

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).

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.