Skip to content
← Back to Community
Learn Node.js in less than an hour [Tutorial]
Profile icon
RohilPatel

Node.js Tutorial

What is the syntax like?

Well, everything you already know with JavaScript can be played here, except for the DOM, which if you don't know, includes interacting with a website.

For example we can do some basic console.logs() in Node.js

const x = 6; // Console commands work as normal console.log('x is ' + 6); // Returns 'x is 6' //But with ES5 you can do this: console.log(`x is ${x}`); // Returns 'x is 6'

Ok, but why Node.js

Well, first off, if you want to know why not to use python, I won't say anything, but if you are here because you don't know the difference between the browser js, then continue on.

Node.js makes everything come together. It's a server, which basically controls a bunch of people. Chat rooms send a message to the server, which sends it to everyone else.

Sign in pages send your typed data and check a database to see if it exists.

But it all starts here...

Node.js has something called a module

4 concepts to remember about modules

  • A module must be imported
  • Modules make Node.js work not like browser js
  • Modules can make coding cleaner, resulting in more files and folders
  • You can make a module very easily

Ok, how do you import a module

Well lets use the old fashion way because repl needs to update to node 14 for other things.

Below is how you import a module

const variableToHoldContentOfModule = require('module name as it should be')

Can we import a module as an example?

Yeah ok, lets use the OS module

It can help with determining what module to use.

// Import the module, using require syntax let os = require('os'); //now that we have the os module, we can use the docs to help us find out our platform. Do this by tacking on .platform() to it. console.log(`Your platform is ${os.platform()}`) //now on repl it will be weird to some, as it says Linux, but thats because repl uses a virtual machine built on linux.

How do we make our own module?

Well modules are just js files that work with the server. Try going into module.js at this time, or making it if you are following by yourself.

In there, we added the following code:

// Write some code that you want to share with index.js, let x = 'some random text to export'; function add(x, y) { return x + y; //simle add function } function sub(x, y) { return x - y; //simle subtract function }

As of right now these functions and variables are local to the file, so to make them public, you do the following after everything was declared. It is usually the last thing in a file.

module.exports = { addFunction: add, subFunction: sub, randomText: x }

Come back to index.js and import it

//Node already knows its a js file so you dont need the extension const moduleWeGot = require('./module'); // the "./" refers to the root directory

// Time to see our options:

console.log(moduleWeGot);

We see this:

{ addFunction: [Function: add], subFunction: [Function: sub], randomText: 'some random text to export' }

Our data is there, so now we can just get them with some dot notation.

moduleWeGot.addFunction(5, 4); //returns 9

There is an easier way to get the module though

const { addFunction, subFunction, randomText } = require('./module');

Then use the function as follows

addFunction(5, 66); //Returns 71

I really hope this tutorial helps!!

Voters
Profile icon
yamin2000
Profile icon
the-o
Profile icon
neiljwise
Profile icon
ShervinBashar
Profile icon
ollieomlette
Profile icon
Yoplayer1py
Profile icon
bisottofelipe64
Profile icon
Kpnumber9
Profile icon
G0RG3
Profile icon
G92708
Comments
hotnewtop
Profile icon
EpicGamer007

I would not call this a node tutorial. I would call it a module tutorial. You don't actually go over anything but module's

Profile icon
[deleted]

True, but luckily there was a module here I didn't actually know about.

Profile icon
RohilPatel
Profile icon
RayhanADev

Well, this isn’t really a NodeJS tutorial, you don’t go over anything that NodeJS is actually used for (Web Dev, App Launch, etc.) but instead just modules..

Profile icon
RohilPatel

Clickbait title lol. Definitely got u lmao @RayhanADev

Profile icon
RayhanADev

@RohilPatel lmao, fits perfectly with this xD.

Profile icon
RohilPatel

Lamo. I've never gotten 100+ upvotes on any of my posts lol @RayhanADev

Profile icon
RayhanADev

@RohilPatel I only got 50+ on the Repl Search Bar. That was it. All my other projects get like 5-20 upvotes. It makes somewhat sad xD.

Profile icon
RohilPatel

Lol, on average I get like 20, but my most has 73 I think @RayhanADev

Profile icon
RayhanADev

@RohilPatel nice! How long have you been on Repl.it anyways? You seem like an OG.

Profile icon
RohilPatel

Nah, I'm not lol. Only been here for like eh, a year and a half @RayhanADev

Profile icon
RayhanADev

@RohilPatel that at least 18 times longer than me also you go to the same school as Coder100?

Profile icon
RohilPatel

Yeah lol. He isn't as magical as he seems though lol in person, and to be fair, nor am I @RayhanADev

Profile icon
RayhanADev

@RohilPatel lmao, Repl is amazing like that isn’t it. I’m becoming scarily well-known for the less than month I’ve been here and irl nobody cares about me xD.

Profile icon
RohilPatel

Lolz. I'm working on a game called Amang Us. (Not a typo) @RayhanADev

Profile icon
RayhanADev

@RohilPatel hahahaha noice one! What’s it going to be about (please don’t say terminal among us I will cry)?

Profile icon
RohilPatel

Nonono, it's a website. I'm scared to make it multiplayer but I will do it due to socket.io resets but look here for now https://amang-us.rohilpatel.repl.co/# @RayhanADev

Profile icon
RayhanADev

@RohilPatel nice (enjoys clicking screen that does nothing) :ramen-blob:

Profile icon
RohilPatel
Profile icon
Mohammad-Fahad2

The place of an instructional exercise is to make sense of how to follow through with something. No place in this instructional exercise does it clear up anything about how for do server-side things.https://www.omegle.fyi/
https://www.chatrandom.one/
https://www.bazoocam.fyi/

Profile icon
[deleted]

Congratulations! This tutorial now has more upvotes than the React tutorial! 😂

Profile icon
[deleted]

Great tuorial, @RohilPatel! I learned a lot that I didn't already know in the os module section.

Profile icon
Bookie0

Cool!

Also i think this comment supposed to be in the code block!
D22D70AA-5C17-46C6-8BF4-9275AD8F71F2

Tonight I plan to have it out, and if all this makes sense, go to here.

Btw there’s no link on the ‘here’, that intentional?

:)

Profile icon
RohilPatel

Yeah I wasn't feeling it. Also thanks for that error, I just made the llesson on the code and then did the md @Bookie0

Profile icon
TANMAYBAGADIA

Thx for this tutorial ,i will see this properly later but it's amazing . Take my upvote!!

Profile icon
RohilPatel
Profile icon
RohilPatel

@DynamicSquid do teachers at squid school make you learn from tutorials?

(Also do you have discord?)

Profile icon
firefish

@RohilPatel The only answer I know is to the latter question and no, he does not have discord. and also try checking timezones, (Mountain Time to be precise)

Profile icon
RohilPatel
Profile icon
RohilPatel

Did u know tutorials like tutorials

Profile icon
TsunamiOrSumth

Good job!

Profile icon
RohilPatel

Thanks! Upvote if you want, and I also saw on someone's post u were looking for a node tutorial, does this help? @tsunami21

Profile icon
TsunamiOrSumth
Profile icon
CodeLongAndPros

@RohilPatel I'm removing this, don't ask for upvotes.

Profile icon
Theboys619

We use Deno here.

Profile icon
RohilPatel
Profile icon
firefish

@RohilPatel you forgot the other way of importing:

import "telephone"; // what the hell why am i making up module names HELP ME from "fs" import "readFileSync"; //at least i thought of one // etc
Profile icon
Coder100

No, readFileSync is an identifier and this is not python, you got them reversed @firefish

Profile icon
RohilPatel
Profile icon
RohilPatel

Python is on the opposite spectrum of JS lmao @Coder100

Profile icon
realTronsi

ha modules is also node's downfall.

Profile icon
RohilPatel

Yeah import is much better =/. Did u like the repl? @realTronsi

Profile icon
RohilPatel

Or I mean the tutorial @realTronsi

Profile icon
realTronsi

@RohilPatel not much content but yeah decent tutorial

Profile icon
RohilPatel

Yeah lol, upvote if u want. Is there anything you think I should make? I'm far more advanced than this tut @realTronsi

Profile icon
Theboys619

quick steps with functions, variables, loops, and if statements / switch statements. Maybe even JSON objects. @RohilPatel

Profile icon
realTronsi

@Theboys619 lol those are too beginner

Profile icon
Theboys619

i guess. @realTronsi

Profile icon
RohilPatel

Yea a little beginner, I'll probably teach ejs and data passing @realTronsi

Profile icon
realTronsi

@RohilPatel hm lol ok

Profile icon
RohilPatel

lolololololol @realTronsi

Profile icon
firefish

Yea a little beginner, I'll probably teach ejs and data passing @realTronsi

@RohilPatel I like to think I know a lot about node, but like what is ejs and data passing

Profile icon
realTronsi

@firefish ejs is just a rendering template, and I don't know what he means by data passing lol

Profile icon
firefish

@realTronsi I had a look at some ejs code, and pug looks more elegant than <%= stuff.

Profile icon
realTronsi

@firefish I never really used pug, but the ejs tag allows you to do expressions and stuff, not sure if pugjs is the same

Profile icon
firefish

@realTronsi like what kind of expressions, but I'm not exactly an expert at pugjs, @Coder100 is however

Profile icon
Coder100

pug js more like EJS @firefish

Profile icon
Coder100

Ok C tutorial when @firefish

Profile icon
realTronsi

@Coder100 ejs more like using websockets to send variables over and then replacing the innerHTML with the variable smort

Profile icon
realTronsi

@Coder100 Why do you need C tutorial

Profile icon
Coder100

Because C good @realTronsi

Profile icon
realTronsi

@Coder100 I thought you know C

Profile icon
firefish

@Coder100 C? YES POG POG trying to speak cookeylang POG POG C TUTORIAL POG POG

Profile icon
realTronsi

@firefish how much C do you know

Profile icon
firefish

@realTronsi moderate amount, here is most of my nkowledge concentrated into one repl: https://repl.it/@firefish/myMem

Profile icon
realTronsi

@firefish wait lol what is this for:

int qRand() { int q[2] = { num_q, 35006 }; return q[3]; }
Profile icon
firefish

@realTronsi Index-out-of-range is not prohibited.

Profile icon
realTronsi

@firefish I know so why is that your function?

Profile icon
Coder100

POG POG POG POOGG @firefish

Profile icon
Coder100

that's why im writing it @realTronsi

Profile icon
firefish

@realTronsi random number generator

Profile icon
firefish

poogg? what in the world @Coder100

Profile icon
realTronsi

no but your random number generator is returning an invalid index, its going to seg fault no? @firefish

Profile icon
firefish

@realTronsi Aha, you see. It doesn't segfault because the index is only two places away from the end of the array, so there is stuff in accessible memory there. A SegFault occurs when you attempt to access memory that is inaccessible to your process, but the memory I am accessing is so close to the end of the array that it's probably reading the string passed to a printf call or something.

Profile icon
realTronsi

@firefish oh so it isn't random then though?

also thought this seg faulted but apparently this doesnt:

#include <stdio.h> int main(void) { int foo[2] = {1, 2}; printf("%d", foo[3]); }
Profile icon
realTronsi

@firefish

#include <stdio.h> int main(void) { int foo[2] = {1, 2}; printf("%d", foo[2]); }

interesting result

Profile icon
firefish

@realTronsi Well, the randomness occurs at compile-time, in the form of how to arrange the binary, rather than at runtime.

Profile icon
firefish

@realTronsi is 4195632 interesting at all?

Profile icon
realTronsi

@firefish what? I don't see how pseudorandomness can occur from compiling?

Profile icon
realTronsi

@firefish not sure what it comes from, doesn't change from manipulating the list

Profile icon
firefish

@realTronsi Ah, just realised. clang does things differently than to gcc.
image

Profile icon
firefish

@realTronsi I know, the clang version works as expected however the gcc one does not

Profile icon
firefish

@realTronsi @fuzzyastrocat Nightmare code: malloc bomb + fork bomb
image

Profile icon
realTronsi

@firefish what is that syntax highlighting ewww

Profile icon
firefish

@realTronsi my theme, thanks for reminding me that it is not normal syntax highlighting

Profile icon
firefish
Profile icon
realTronsi

@firefish ah yes perfection

Profile icon
fuzzyastrocat

@RohilPatel Please actually cover node's strength and main use case: writing server applications.

Profile icon
realTronsi

@fuzzyastrocat and also having simultaneously the worst and best package manager

Profile icon
RohilPatel
Profile icon
RohilPatel

Data passing is where you send something from the backend to the front end.

app.get('/', (_, res) => { res.render('index', { x: 5, y: 6, z: 9 } }
<%= x %> <!-- Returns "5" -->

@realTronsi

Profile icon
realTronsi

oh, just cover templating engines instead then @RohilPatel

Profile icon
RohilPatel

yeah probably. @realTronsi

Profile icon
fuzzyastrocat

@RohilPatel Uh, where? All I see in the tutorial is modules.

Profile icon
RohilPatel

In the "ok, why NodeJs" @fuzzyastrocat

Profile icon
fuzzyastrocat

@RohilPatel The point of a tutorial is to explain how to do something. Nowhere in this tutorial does it explain anything about how to do server-side things.