Skip to content
Sign upLog in
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
29

Learn Node.js in less than an hour [Tutorial]

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

2 years ago

Voters

Comments

TopNew
1
hamzamaqsood3
hamzamaqsood3

Yeah, it does. I was how to get fiberglass out of skin surprised too. Even more surprised when I found out it can do links too. Like a markdown link with custom text. IT DOES THAT.

5 months ago
1
Mohammad-Fahad2
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/

7 months ago
1

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

2 years ago
2
RayhanADev
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..

2 years ago
1
RohilPatel
RohilPatel

Clickbait title lol. Definitely got u lmao

@RayhanADev

2 years ago
1
RayhanADev
RayhanADev

@RohilPatel
lmao, fits perfectly with this xD.

2 years ago
1
RohilPatel
RohilPatel

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

@RayhanADev

2 years ago
1
RayhanADev
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.

2 years ago
1
RohilPatel
RohilPatel

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

@RayhanADev

2 years ago
1
RayhanADev
RayhanADev

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

2 years ago
1
RohilPatel
RohilPatel

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

@RayhanADev

2 years ago
1
RayhanADev
RayhanADev

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

2 years ago
2
RohilPatel
RohilPatel

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

@RayhanADev

2 years ago
1
RayhanADev
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.

2 years ago
1
RohilPatel
RohilPatel

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

@RayhanADev

2 years ago
1
RayhanADev
RayhanADev

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

2 years ago
1
RohilPatel
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

2 years ago
1
RayhanADev
RayhanADev

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

2 years ago
7
EpicGamer007
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

2 years ago
1

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

2 years ago
1

Great tuorial,

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

2 years ago
1
Bookie0
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?

:)

2 years ago
1
RohilPatel
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

2 years ago
1
TANMAYBAGADIA
TANMAYBAGADIA

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

2 years ago
1
RohilPatel
RohilPatel

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

(Also do you have discord?)

2 years ago
1
firefish
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)

2 years ago
1
RohilPatel
RohilPatel

Did u know tutorials like tutorials

2 years ago
Load more