Download as pdf or txt
Download as pdf or txt
You are on page 1of 300

nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-1

Node JS
INTRODUCTION
nerd_jfpb
nerdjfpb nerdjfpb

Node.js is an open-source,
cross-platform, JavaScript
runtime environment
that executes JavaScript
code outside of a browser.
(From Wikipedia)
HARD TO UNDERSTAND RIGHT?

nerd_jfpb
nerdjfpb nerdjfpb

-------
If I just give a
short INTRO
about it then
-------

nerd_jfpb
nerdjfpb nerdjfpb

A PLATFORM
which allows us
to run JAVASCRIPT
on a computer/server

nerd_jfpb
nerdjfpb nerdjfpb

CRUD options for


files (Create, Read,
Update, Delete)

Can use with


any type of
DATABASE

nerd_jfpb
nerdjfpb nerdjfpb

Why
NODE is
so popular?

JAVASCRIPT
everywhere

nerd_jfpb
nerdjfpb nerdjfpb

SUPER FAST

LIGHTWEIGHT

nerd_jfpb
nerdjfpb nerdjfpb

Huge
ECOSYSTEM

Great
for real time
application

nerd_jfpb
nerdjfpb nerdjfpb

HIGH
performance

Easy to
LEARN

nerd_jfpb
nerdjfpb nerdjfpb

NOW TELL ME
DO YOU LIKE
NODE JS ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-2

Install NODE JS
nerd_jfpb
nerdjfpb nerdjfpb

Go to node website
https://nodejs.org/en/

nerd_jfpb
nerdjfpb nerdjfpb

In my pc node was installed


before which is 10.16.3
but you can install 12.13.1
(LTS Recommended For Most
Users)
because LTS means
Long Term Support

nerd_jfpb
nerdjfpb nerdjfpb

Download and install

nerd_jfpb
nerdjfpb nerdjfpb

Now open command promt


and write `node -v`
then you'll see
what version use are using
of node.

nerd_jfpb
nerdjfpb nerdjfpb

create a folder
and name it
whatever you want

nerd_jfpb
nerdjfpb nerdjfpb

create a file
and name it
whatever you want
with .js extention

nerd_jfpb
nerdjfpb nerdjfpb

Write the simple code


in there

nerd_jfpb
nerdjfpb nerdjfpb

Open command promt


on this folder
and write command
`node YOURFILENAME`
like `node app.js`

nerd_jfpb
nerdjfpb nerdjfpb

AND MAGIC!!! SEE THE RESULT!

So did you
installed it properly?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-3

JAVASCRIPT
Engine
nerd_jfpb
nerdjfpb nerdjfpb

WHY WE NEED
JAVASCRIPT ENGINE ?

Because computer is not smart


enough to understand javascript
without javascript engine.

So, Javascript engine work as a


middle man to make computer
understand the javascript.

nerd_jfpb
nerdjfpb nerdjfpb

SO WHAT IS THE
PURPOSE OF
JAVASCRIPT ENGINE ?

Make the javascript codes into


machine/binary codes.

nerd_jfpb
nerdjfpb nerdjfpb

SOME JAVASCRIPT
ENGINES
V8 by Google for Chrome(most used)
SpiderMonkey by Mozila for Firexof
JavascriptCore by Apple for Safari
Chakra by Microsoft for Microsoft
Edge
Hemes engine by Facebook for
Android apps
nerd_jfpb
nerdjfpb nerdjfpb

WHAT IS V8 ?
V8 is an open-source JavaScript
engine developed by The Chromium
Project for Google Chrome and
Chromium web browsers.
The project’s creator is Lars Bak.
The first version of the V8 engine was
released at the same time as the
first version of Chrome: September 2,
2008. (wikipedia)
nerd_jfpb
nerdjfpb nerdjfpb

SO HOW V8 WORKS?

We write codes
It goes to v8
Finally v8 changes it to c++
c++ changed into assembly language
Finally assembly to machine codes

nerd_jfpb
nerdjfpb nerdjfpb

ARE YOU FEELING TIRED


AFTER ALL THESE
INFORMATION ?

One tricky question for you then,


if every company can make their own
version of engine then they should not
make it in their way right ?
Why same javascript codes works
on all browser in a same way ?

nerd_jfpb
nerdjfpb nerdjfpb

TIRED OF SEARCHING
ANWSER ?

nerd_jfpb
nerdjfpb nerdjfpb

Because everyone follow


'ECMAScript'. This is standardized
for writing JavaScript engine!

nerd_jfpb
nerdjfpb nerdjfpb

Do you learnt
something new today ?
Share and comment to
help others too!

nerd_jfpb
nerdjfpb nerdjfpb

Day-4

Learn Node JS
in 30 Days

WINDOW === GLOBAL

nerd_jfpb
nerdjfpb nerdjfpb

IF YOU KNOW JAVASCRIPT


ALREADY THEN YOU KNOW
THERE IS A WINDOW OBJECT
IN CONSOLE

nerd_jfpb
nerdjfpb nerdjfpb

IF YOU NEW TO JAVASCRIPT


THEN JUST OPEN YOUR
CONSOLE IN BROWSER AND
WRITE `WINDOW` THERE
AND YOU CAN SEE WHAT
INSIDE THIS WINDOW OBJECT

nerd_jfpb
nerdjfpb nerdjfpb

NOW WE ARE NOT IN


BROWSER SO WE DON'T
HAVE ANY ACCESS TO
THE WINDOW.
SO WHAT IS IN NODE ?

nerd_jfpb
nerdjfpb nerdjfpb

YOU CAN GO TO LATEST DOC


OF NODE JS BY
https://nodejs.org/dist/latest-v12.x/docs/api/

nerd_jfpb
nerdjfpb nerdjfpb

GOOD THING IS YOU CAN


ACCESS THE `GLOBAL` OBJECT
WHICH IS SAME AS `WINDOW`
https://nodejs.org/dist/latest-v12.x/docs/api/globals.html

nerd_jfpb
nerdjfpb nerdjfpb

NOW YOU CAN USE ALL THE


FUNCTIONS LIKE -
setInterval, setTimeout ETC
ON APP.JS

nerd_jfpb
nerdjfpb nerdjfpb

EXAMPLE

nerd_jfpb
nerdjfpb nerdjfpb

RUN THE CODE BY


`NODE APP.JS`

nerd_jfpb
nerdjfpb nerdjfpb

Did you understand


TODAY'S LESSON ?
You can comment anytime
if you've any question!

nerd_jfpb
nerdjfpb nerdjfpb

Day-5

Learn Node JS
in 30 Days

Function Function
Declarations Expressions

nerd_jfpb
nerdjfpb nerdjfpb

Function Declarations
are normal function
we write!

nerd_jfpb
nerdjfpb nerdjfpb

Example
(FUNCTION DECLARATIONS)

nerd_jfpb
nerdjfpb nerdjfpb

FUNCTION
EXPRESSIONS

The function keyword


can be used to define
a function inside
an expression. [MDN]

nerd_jfpb
nerdjfpb nerdjfpb

Example
(FUNCTION EXPRESSIONS)

nerd_jfpb
nerdjfpb nerdjfpb

Why this is
IMPORTANT ?

nerd_jfpb
nerdjfpb nerdjfpb

Because this pattern


is used widely
in JAVASCRIPT

nerd_jfpb
nerdjfpb nerdjfpb

This will help you


understand and write
small function easily
inside of a
EXPRESSION

nerd_jfpb
nerdjfpb nerdjfpb

Function Declarations
vs.
Function Expressions

nerd_jfpb
nerdjfpb nerdjfpb

SO ARE YOU
ENJOYING
THIS SERIES ?
nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days
Day-6

KEYWORDS
'REQUIRE'
&
'MODULE'
nerd_jfpb
nerdjfpb nerdjfpb

Create two files `app.js`


and `song.js`. you can
create your files as
your desired name.

nerd_jfpb
nerdjfpb nerdjfpb

First we'll write


some codes in
`song.js` file

nerd_jfpb
nerdjfpb nerdjfpb

I know what you are thinking why there is


a extra line `module.exports` in the end of
this file.
This module.exports will available this
function sing to everyone who required it.
So how to require ?

nerd_jfpb
nerdjfpb nerdjfpb

You can just start require


by writing require('./song)
it will automatically
understand it is a js file

nerd_jfpb
nerdjfpb nerdjfpb

Now you can call the


sing file by require it
and store it in a variable

nerd_jfpb
nerdjfpb nerdjfpb

Now you can call


the function from the
`app.js` file

nerd_jfpb
nerdjfpb nerdjfpb

You can run the file


now by command
`node app.js`

nerd_jfpb
nerdjfpb nerdjfpb

Final Result

nerd_jfpb
nerdjfpb nerdjfpb

DO YOU UNDERSTAND
the Require
and module now ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days
Day-7

MORE
'MODULE'

nerd_jfpb
nerdjfpb nerdjfpb

We are using files as last


posts. Now it can happens
that in our `song` folder there
are two different functions.

nerd_jfpb
nerdjfpb nerdjfpb

Now how to send this both


module, as per we know from last
posts we can't send both functions
using this module.exports

nerd_jfpb
nerdjfpb nerdjfpb

Adding
module.exports.VARIABLE_NAME
we can send it to other file

nerd_jfpb
nerdjfpb nerdjfpb

By using
Song.VARIABLE_NAME
what we've send from `song.js` file
we can call the functions

nerd_jfpb
nerdjfpb nerdjfpb

This is not all,


there are some others way
you can do it to.
What if you have
100functions in the
same file are you going to
write one by one ?

nerd_jfpb
nerdjfpb nerdjfpb

Now we can just use


module.exports as a object !

-Example-

nerd_jfpb
nerdjfpb nerdjfpb

`app.js` will be the same and if we


run it now then same result

nerd_jfpb
nerdjfpb nerdjfpb

now we can modify code a bit. When


the name are same in es6 version we can
write just once.

nerd_jfpb
nerdjfpb nerdjfpb

SO, DO YOU FEEL


YOU KNOW A BIT ABOUT
MODULE NOW ?

nerd_jfpb
nerdjfpb nerdjfpb

Day-8

Learn Node JS
in 30 Days

VAR CONST LET

nerd_jfpb
nerdjfpb nerdjfpb

Before es6 `VAR` was the King.


We used var in everywhere
we wants to declare a variable

nerd_jfpb
nerdjfpb nerdjfpb

`var` variables can be


re-declared and updated

nerd_jfpb
nerdjfpb nerdjfpb

`Now we divide
var with
two different types
one CONST &
other LET

nerd_jfpb
nerdjfpb nerdjfpb

CONST is declared
for those value
which will not
changed anytime!
Or will not be updated
at any point

nerd_jfpb
nerdjfpb nerdjfpb

`CONST` cannot be updated


or re-declared
this will give a error
you can't run code

nerd_jfpb
nerdjfpb nerdjfpb

We use LET when their is a


change of updating the
variable

nerd_jfpb
nerdjfpb nerdjfpb

LET can be updated but


not re-declared.
this will give a error
you can't run code

nerd_jfpb
nerdjfpb nerdjfpb

So in es6 we use
LET & CONST
not Var!

nerd_jfpb
nerdjfpb nerdjfpb

Do you knew this


BEFORE ?

nerd_jfpb
nerdjfpb nerdjfpb

Day-9

Learn Node JS
in 30 Days

MASTERING
EVENTEMITTER

nerd_jfpb
nerdjfpb nerdjfpb

Events (Core module of node js)


https://nodejs.org/api/events.html

nerd_jfpb
nerdjfpb nerdjfpb

First we need to require the


`events`

nerd_jfpb
nerdjfpb nerdjfpb

Now we can do something for


clicking any element.

nerd_jfpb
nerdjfpb nerdjfpb

This `event` module


will help us to use
click and different events.
We can now create
custom events also!

nerd_jfpb
nerdjfpb nerdjfpb

For using the custom event first


we'll
`const myEmitter = new events()`
and get new events on myEmitter

nerd_jfpb
nerdjfpb nerdjfpb

Second step is making a new


custom event which is called
`customEvent` in this occassion

nerd_jfpb
nerdjfpb nerdjfpb

Then finally we need to make


this event happens using
`myEmitter.emit`

nerd_jfpb
nerdjfpb nerdjfpb

FINAL RESULT

nerd_jfpb
nerdjfpb nerdjfpb

So do you understand
a bit of EventEmitter
right now ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-10

File
READ & WRITE
using node api fs
nerd_jfpb
nerdjfpb nerdjfpb

Go to
https://nodejs.org/dist/latest-v12.x/docs/api/fs.html
then you can see oficial document
of fs

nerd_jfpb
nerdjfpb nerdjfpb

Now,
first we need to require the fs
and store it on a const by writing
`const fs = require('fs')`

nerd_jfpb
nerdjfpb nerdjfpb

We are going to use


`fs.readFileSync`

nerd_jfpb
nerdjfpb nerdjfpb

I am using a `song.txt` file


on same folder and read file
text using `readFileSync`
and console log it

nerd_jfpb
nerdjfpb nerdjfpb

Run it and see the magic

nerd_jfpb
nerdjfpb nerdjfpb

We are going to use


writeFileSync for the writing
in song.txt file

nerd_jfpb
nerdjfpb nerdjfpb

Using fs.writeFileSync
(path, dataForWrite)
we can write in the file,
what we need to write.

nerd_jfpb
nerdjfpb nerdjfpb

After write, we need to read


the data so that we can see
if we write in file properly

nerd_jfpb
nerdjfpb nerdjfpb

DID YOU USED


FS MODULE BEFORE ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-11

Creating
and
Deleting Folders
nerd_jfpb
nerdjfpb nerdjfpb

We are on the same fs module


for the creating and deleting folders
https://nodejs.org/dist/latest-v12.x/docs/api/fs.html

nerd_jfpb
nerdjfpb nerdjfpb

We are going to use `fs.mkdirSync`


to make new folder

nerd_jfpb
nerdjfpb nerdjfpb

I'm creating a new folder called


`song` by writing fs.mkdirSync

nerd_jfpb
nerdjfpb nerdjfpb

Run it using `node app.js`


It created a new song folder
on the dir

nerd_jfpb
nerdjfpb nerdjfpb

Now we are going to use


`fs.rmdirSync` to delete the folder

nerd_jfpb
nerdjfpb nerdjfpb

We can delete dir by


fs.rmdirSync(path) so we'll try it

nerd_jfpb
nerdjfpb nerdjfpb

Run it using `node app.js`

nerd_jfpb
nerdjfpb nerdjfpb

So
we created and deleted folder
easily using `fs.mkdirSync`
and `fs.rmdirSy

nerd_jfpb
nerdjfpb nerdjfpb

DO YOU HAVE
ANY QUESTIONS ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-12

Creating
OWN SERVER

nerd_jfpb
nerdjfpb nerdjfpb

Client make a request to the server


and server provide the files using
socket.
So we are going to learn about
how to make our own server using
node today!

nerd_jfpb
nerdjfpb nerdjfpb

We are going to use http module


for this.
You can find the offical document here
https://nodejs.org/dist/latest-v12.x/docs/api/http.html

nerd_jfpb
nerdjfpb nerdjfpb

First we are going to require http


and store it to http variable using
`const http = require('http')`

nerd_jfpb
nerdjfpb nerdjfpb

We are going to use


http.createServer for creating
the server

nerd_jfpb
nerdjfpb nerdjfpb

We can just start our server using


`const server = http.createServer()`
but there are some more work to do.

nerd_jfpb
nerdjfpb nerdjfpb

We need to write a function inside


the createServer() so that we can
get the request and response
according about it

nerd_jfpb
nerdjfpb nerdjfpb

now we can send data to request


using `res.end('Hey everyone!')`
and we need to give a port to listen.
In my case I'm using 3000.

nerd_jfpb
nerdjfpb nerdjfpb

Now run the code using `node app.js`

nerd_jfpb
nerdjfpb nerdjfpb

and finally go to localhost:3000


and you can see the result

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-13

BUFFER
&
STREAM
nerd_jfpb
nerdjfpb nerdjfpb

Do you know What is buffer ?

nerd_jfpb
nerdjfpb nerdjfpb

Temporary storage spot for a chunk


of data that can be transferred
a small amount at a time

DATA

BUFFER
DATA
IS
GOING

nerd_jfpb
nerdjfpb nerdjfpb

Buffer class is a global class


that can be accessed
in an application
without importing the
buffer module.

nerd_jfpb
nerdjfpb nerdjfpb

There are some ways


you can call buffer.

nerd_jfpb
nerdjfpb nerdjfpb

Do you know what is stream ?

nerd_jfpb
nerdjfpb nerdjfpb

Streams are objects


that let you read data
from a source
or
write data
to a destination
in continuous fashion.

nerd_jfpb
nerdjfpb nerdjfpb

There are four type of stream


in node js.
a) Readable −
Stream which is used for
read operation.
b) Writable −
Stream which is used for
write operation.

nerd_jfpb
nerdjfpb nerdjfpb

(OTHER TWO)
c)Duplex −
Stream which can be used for both
read and write operation.
d)Transform −
A type of duplex stream where
the output is computed based
on input.

nerd_jfpb
nerdjfpb nerdjfpb

Do you knew this


'Buffer' & 'Stream' before ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-14

SERVING
HTML PAGES

nerd_jfpb
nerdjfpb nerdjfpb

Do you know
we can send html pages
to the client?

nerd_jfpb
nerdjfpb nerdjfpb

Yes! You can do it using node js.

nerd_jfpb
nerdjfpb nerdjfpb

First we'll create


the server as like day 12

nerd_jfpb
nerdjfpb nerdjfpb

For sending the html file we need


the fs module which in built in node.

nerd_jfpb
nerdjfpb nerdjfpb

I got the starter from bootstrap -


https://getbootstrap.com/docs/4.4/getting-started/introduction/

nerd_jfpb
nerdjfpb nerdjfpb

We need to create stream using


fs module. So that we can send
the html file.

nerd_jfpb
nerdjfpb nerdjfpb

Then we can send it using pipe


and send the response.

nerd_jfpb
nerdjfpb nerdjfpb

NOW RUN IT USING `NODE APP.JS`

nerd_jfpb
nerdjfpb nerdjfpb

See the result

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-15

Sending JSON
to Client

nerd_jfpb
nerdjfpb nerdjfpb

Do you know we can send JSON


object to the client side ?

And we mostly use JSON data to


pass client side.

nerd_jfpb
nerdjfpb nerdjfpb

We'll do it using `res.end()`

nerd_jfpb
nerdjfpb nerdjfpb

We need to create a server first.


You can see it from day 12.

nerd_jfpb
nerdjfpb nerdjfpb

Now we can create an JSON object

nerd_jfpb
nerdjfpb nerdjfpb

Now change the Content-Type

nerd_jfpb
nerdjfpb nerdjfpb

We can send the data to frontend


now using `res.end(JSONObject)`

nerd_jfpb
nerdjfpb nerdjfpb

`JSON.stringify()` A common use of


JSON is to exchange data to/from
a web server.

nerd_jfpb
nerdjfpb nerdjfpb

Called server using postman -

nerd_jfpb
nerdjfpb nerdjfpb

Can you send JSON data


using node js now ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-16

Routing (Basic)

nerd_jfpb
nerdjfpb nerdjfpb

We've already created basic server


before so we already know how to
start a server and get reponse.
But how we can go on a specific
route ?

nerd_jfpb
nerdjfpb nerdjfpb

Solution is not that hard actually.


We can check req.url first,
which url client is asking for,
then we can provide
the data basis on that.

nerd_jfpb
nerdjfpb nerdjfpb

To make the routing,


first we can check the request url
using if and send some reponse
based on that.

nerd_jfpb
nerdjfpb nerdjfpb

Run it using `node app.js`


and check in browser
http://localhost:3000/home

nerd_jfpb
nerdjfpb nerdjfpb

Now we can easily add new page.


ex- about us page

nerd_jfpb
nerdjfpb nerdjfpb

Stop the server and re-run again


using `node app.js`

nerd_jfpb
nerdjfpb nerdjfpb

We can add more


pages like this.
But what if someone
request a url we didn't listed ?

nerd_jfpb
nerdjfpb nerdjfpb

Solution is pretty simple just add


a else for that and send
tell them not found.

nerd_jfpb
nerdjfpb nerdjfpb

Finally you can check using the


wrong url.

“SO CAN YOU WRITE


ROUTES NOW ?”

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-17

Npm -
Node Package
Manager

nerd_jfpb
nerdjfpb nerdjfpb

NPM is a package manager


for the JavaScript programming
language.
It is the default package manager
for the JavaScript runtime
environment Node.js.
(wikipedia)

nerd_jfpb
nerdjfpb nerdjfpb

UNDERSTAND ANYTHING ?

nerd_jfpb
nerdjfpb nerdjfpb

A package manager or
package-management system is a
collection of software tools that
automates the process of installing,
upgrading, configuring, and removing
computer programs for a computer's
operating system in a consistent
manner. (wikipedia)

nerd_jfpb
nerdjfpb nerdjfpb

So basically npm is a system


what we can use to do different
things with our codebase to manage
properly.
https://www.npmjs.com/

nerd_jfpb
nerdjfpb nerdjfpb

Now it's time to use npm.


We can start configure a project
using `npm init`.
It will promt up some question and
most basically we are going to say
yes to them.
So shortcut way is just add -yes
after everything. like - `npm init -yes`

nerd_jfpb
nerdjfpb nerdjfpb

Now we'll see there is a new file


call `package.json`
In `package.json` we can see all the
data about the npm created for us.
Now we're ready to use npm.

nerd_jfpb
nerdjfpb nerdjfpb

We are going to use


express-js(node js framework)
for further tutorials.
Now let's install it
now current project.

nerd_jfpb
nerdjfpb nerdjfpb

Honestly it's so easy.


Just use the cmd and type npm
install express.

nerd_jfpb
nerdjfpb nerdjfpb

Check the `package.json` file.

“WOW!!! YOU'VE INSTALLED


A PACKAGE USING NPM!”
nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days
Day-18

Why
Express JS ?
nerd_jfpb
nerdjfpb nerdjfpb

In last tutorial we learnt about


installing express js using npm.
But honestly why express js and
what are options we have ?

nerd_jfpb
nerdjfpb nerdjfpb

Frameworks helps us to write code


easily and maintain a standard
of our codes.
This really needed when multiple
team member are working
on the same code.

nerd_jfpb
nerdjfpb nerdjfpb

Can you think how many node js


framework are available right now ?

nerd_jfpb
nerdjfpb nerdjfpb

Actually many!
I didn't find a number to tell you.
But I can listed some -
- Express.js
- Nest.js
- Sails.js
- Koa.js
and many more

nerd_jfpb
nerdjfpb nerdjfpb

See survery of 2018 ---


https://2018.stateofjs.com/back-end-frameworks/overview/

Express js is dominating when it's


time for a backend framework

nerd_jfpb
nerdjfpb nerdjfpb

Learning and using popular


framework has a good benefits
honestly.
Because there is a huge community
who can help you, also you can get
some decent jobs on the popular
framework.
But is this all the reason why I choose
express ?

nerd_jfpb
nerdjfpb nerdjfpb

No there are more.


SWIPE TO KNOW

nerd_jfpb
nerdjfpb nerdjfpb

Express is
- Fast & Simple
- Speedy
- Scalable
- Easy to Learn

nerd_jfpb
nerdjfpb nerdjfpb

SO ARE YOU INTERESTED


TO LEARN
MORE ABOUT EXPRESS ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days
Day-19

NODEMON
nerd_jfpb
nerdjfpb nerdjfpb

Up until now we've faced an issue.


Did you noticed it already ?

nerd_jfpb
nerdjfpb nerdjfpb

Everytime we change some part


of our code
and we had to re-run
again right ?
Isn't it tiring ?

nerd_jfpb
nerdjfpb nerdjfpb

We can easily end this re-run


process by using a package
call nodemon.
So how to use this nodemon ?

nerd_jfpb
nerdjfpb nerdjfpb

Check out the docs first


about the nodemon.
https://www.npmjs.com/package/nodemon

nerd_jfpb
nerdjfpb nerdjfpb

`npm install -g nodemon`


will installed nodemon,
but what is the `-g` ?
Up until now we never saw
something like this right ?
`-g` means global.
Installing globally means
we use this package
from any npm projects.

nerd_jfpb
nerdjfpb nerdjfpb

There is two way we can now use the


nodemon. Easiest one is - just simply
write nodemon in the console and it
will start the server.

Nodemon is smart enough to point


app.js when you just call it using
nodemon, you can write nodemon
app.js too.
nerd_jfpb
nerdjfpb nerdjfpb

The second way to use script on the


`package.json` file. We are making a
start script using nodemon.

nerd_jfpb
nerdjfpb nerdjfpb

Now write `npm start` in console


and we're getting the exact same
thing like before.

nerd_jfpb
nerdjfpb nerdjfpb

Now we can change anything


and save it,
nodemon will automatically restart
the server for us
with new changes.
SO ARE YOU GOING TO
USE NODEMON ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days
Day-20

EXPRESS JS
INTRODUCTION

nerd_jfpb
nerdjfpb nerdjfpb

We can start using checking


the official document of express js
https://expressjs.com/

nerd_jfpb
nerdjfpb nerdjfpb

We are going to use express


mostly for 3 reasons.
- Easy routing system
- Can use with many templating
system
- Have a middleware framework

nerd_jfpb
nerdjfpb nerdjfpb

We can start with the routing.


Before express we write a long code
to create a server, but with express
it's pretty easy. See the old code

nerd_jfpb
nerdjfpb nerdjfpb

Now we are going to write


the same old code
using express

nerd_jfpb
nerdjfpb nerdjfpb

First we need to `require express`


then create an app constant using
`express()`

nerd_jfpb
nerdjfpb nerdjfpb

Now we can write app.listen(3000)


to listen 3000 port.

nerd_jfpb
nerdjfpb nerdjfpb

We can write the routes now.


Like homepage -

nerd_jfpb
nerdjfpb nerdjfpb

Now our code is more readable and


easy to add any route.

nerd_jfpb
nerdjfpb nerdjfpb

SO DO YOU LIKE
THE INTRODUCTION
OF EXPRESS JS ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days
Day-21

HTTP
METHODS

nerd_jfpb
nerdjfpb nerdjfpb

Http methods are


kind of request we made
to the server.

nerd_jfpb
nerdjfpb nerdjfpb

We are most going to


use 4
for our system

nerd_jfpb
nerdjfpb nerdjfpb

GET - This is for getting data


from server
like - We can get a anime list
from server

in express js we use `app.get`


for this type request.

nerd_jfpb
nerdjfpb nerdjfpb

POST - This method is for sending


something to server.
Like - We can send form data to
server

in express js we use `app.post`


to this type request.

nerd_jfpb
nerdjfpb nerdjfpb

DELETE - This method is delete


something in server
Like - we can delete a anime from
the list

in express js we use `app.delete`


to this type request.

nerd_jfpb
nerdjfpb nerdjfpb

PUT - This is for updating something


like - we can update an anime name,
for mis-spell

in express js we use `app.put`


to this type request.

nerd_jfpb
nerdjfpb nerdjfpb

REST API use this http protocol


to send data over computers.

nerd_jfpb
nerdjfpb nerdjfpb

So we already wrote REST API


by using
app.get, app.post, app.delete
&
app.put

nerd_jfpb
nerdjfpb nerdjfpb

DID YOU LEARN


SOMETHING NEW TODAY ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-22

ROUTE
PARAMETERS

nerd_jfpb
nerdjfpb nerdjfpb

Up until now
we never passed any id
with our route.
What if we need
to go to a specific profile ?

nerd_jfpb
nerdjfpb nerdjfpb

Like - `profile/10`
How we can load different
using those id ?

nerd_jfpb
nerdjfpb nerdjfpb

We can get what user request


after profile,
how we can get it ?
In this case
they request for the 10 right ?

nerd_jfpb
nerdjfpb nerdjfpb

To catch the 10
we need to make a specific route
like '/profile/:id'
this id will catch
what request send after profile.
So how to catch it ?

nerd_jfpb
nerdjfpb nerdjfpb

Just use `req.params.id` this will


give us the id

nerd_jfpb
nerdjfpb nerdjfpb

See the result on the browser

nerd_jfpb
nerdjfpb nerdjfpb

So we get the details using


req.params right ?
So what is req.params ?
we can easily console
log it and what we got ?

nerd_jfpb
nerdjfpb nerdjfpb

An object contain the extra


parameters we send with the url.

nerd_jfpb
nerdjfpb nerdjfpb

NOW YOU KNOW


ABOUT USING PARAMS,
SO
YOU CAN CREATE
THE PROFILE PAGE
RIGHT ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-23

TEMPLATING
ENGINE

nerd_jfpb
nerdjfpb nerdjfpb

To creating
QUICK NODE APPLICATION
we can use some
FRONTEND TEMPLATE ENGINE.

nerd_jfpb
nerdjfpb nerdjfpb

There are many different engines


- EJS
- Mustache
- Handlebars
- Underscore
- Pug
- Marko

nerd_jfpb
nerdjfpb nerdjfpb

We can use any of them


TO DO THE FRONTEND
WHILE MAKING THE API.

nerd_jfpb
nerdjfpb nerdjfpb

I'm going to use EJS


to show you
HOW TO DO THIS.

nerd_jfpb
nerdjfpb nerdjfpb

Start with the official page


https://ejs.co/

nerd_jfpb
nerdjfpb nerdjfpb

You can check the features of it

nerd_jfpb
nerdjfpb nerdjfpb

Installing it is super easy

nerd_jfpb
nerdjfpb nerdjfpb

So finally our `package.json`

nerd_jfpb
nerdjfpb nerdjfpb

WANT TO KNOW
MORE ABOUT
TEMPLATING ENGINE ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-24

WORKING WITH
EJS - PART 1

nerd_jfpb
nerdjfpb nerdjfpb

Hope you've installed the ejs.


We're going to learn some more
things about it. To start with the
ejs first we need to set our app to
use it. We can simply add this using
`app.set('view engine', 'ejs')`

nerd_jfpb
nerdjfpb nerdjfpb

This
`app.set('view engine', 'ejs')`
will look into views folder
on the same path.
So we need to create
views folder
for writing those
frontend templates.

nerd_jfpb
nerdjfpb nerdjfpb

We are starting the


profile page.
So we'll create a
`profile.ejs` file inside
the views folder.

nerd_jfpb
nerdjfpb nerdjfpb

Just create a basic html inside of


`profile.ejs` file.

nerd_jfpb
nerdjfpb nerdjfpb

We'll use res.render method to


render the file.

nerd_jfpb
nerdjfpb nerdjfpb

It's already know where to look.


So we've render the files,
but we didn't pass our data to the
html. How to do that ?

nerd_jfpb
nerdjfpb nerdjfpb

We can send a object in render


method while rendering the template
and use that object to get the value
of username.

nerd_jfpb
nerdjfpb nerdjfpb

Now we can easily access the value


using <%= person %>
in the `profile.ejs` file

nerd_jfpb
nerdjfpb nerdjfpb

See the result in frontend

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-25

WORKING WITH
EJS - PART 2

nerd_jfpb
nerdjfpb nerdjfpb

Hope you understand


a bit of EJS now.
Well, we have a problem right now.
What if we need to pass
multiple data to the view?
What to do then ?

nerd_jfpb
nerdjfpb nerdjfpb

Don't worry solution is easy.


Let's suppose,
we'll show the person age,
job and hobbies in the
profile page.
So how can we do it ?

nerd_jfpb
nerdjfpb nerdjfpb

First we need to create an object


which contains all the data.
So just call it data.

nerd_jfpb
nerdjfpb nerdjfpb

Now we can pass the data after


person using render function.

nerd_jfpb
nerdjfpb nerdjfpb

Let's start with the age.


We'll print the age in the frontend

nerd_jfpb
nerdjfpb nerdjfpb

See the result in browser

nerd_jfpb
nerdjfpb nerdjfpb

Now let finish the whole thing, it's


pretty easy but we need to know that
to write javascript we use <% to start
and %> to end and for printing a
variable value we use <%= variable %>

nerd_jfpb
nerdjfpb nerdjfpb

See the result in browser

nerd_jfpb
nerdjfpb nerdjfpb

SO DO YOU THINK
YOU CAN MAKE
SOME SIMPLE PROJECT
RIGHT NOW ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-26

WORKING WITH
PARTIAL EJS
- PART 3
nerd_jfpb
nerdjfpb nerdjfpb

We already know about templating


engine right?
Now tell me one thing,
if you have 10pages,
how you'll handle the navbar ?
Can we reuse the navbar
for everypage ?

nerd_jfpb
nerdjfpb nerdjfpb

Answer is YES.
We can but we need to make
the navbar as partials.
Partials is a concept in ejs.

nerd_jfpb
nerdjfpb nerdjfpb

So create a folder
inside our view folder
call it `partials`.
We'll store the partial files here.

nerd_jfpb
nerdjfpb nerdjfpb

Let's start creating from head files.


Like the cdn's we are going to use.
Let's try bootstrap in our case.

nerd_jfpb
nerdjfpb nerdjfpb

I'm using the navbar from


bootstrap 4 too.
https://getbootstrap.com/docs/4.4/components/navbar/

nerd_jfpb
nerdjfpb nerdjfpb

Let's add the footer also

nerd_jfpb
nerdjfpb nerdjfpb

Now we can easily use this partial


parts easily.

nerd_jfpb
nerdjfpb nerdjfpb

See the result

nerd_jfpb
nerdjfpb nerdjfpb

Do you understand
how we can easily do
many things
using partial templating ?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-27

WORKING WITH
STATIC FILES

nerd_jfpb
nerdjfpb nerdjfpb

Lets do a fun thing.


Start a stylesheet on our profile page.
like -

nerd_jfpb
nerdjfpb nerdjfpb

Now let's write a little bit of css

nerd_jfpb
nerdjfpb nerdjfpb

Add the css class in our current


`profile.ejs` page

nerd_jfpb
nerdjfpb nerdjfpb

now check the browser,


sadly know change.
Do you know the reason ?

nerd_jfpb
nerdjfpb nerdjfpb

Because we can access that


static files. Because we didn't make
any static files route.
Let's add this in `app.js` so that
we can get the static files.

nerd_jfpb
nerdjfpb nerdjfpb

We can use a inbuilt express


middleware to access that static
folder which will created.
Just using
`app.use('/assets', express.static('assets'))`
will give access to the /assets folder.

nerd_jfpb
nerdjfpb nerdjfpb

Now we can go to browser


and see out it change.

nerd_jfpb
nerdjfpb nerdjfpb

In
`app.use('/assets', express.static('assets'))`
In 1st part is route and 2nd part is
folder that we are going to put
the files.

nerd_jfpb
nerdjfpb nerdjfpb

SO,
CAN YOU WORK WITH
THE STATIC FILES NOW?

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-28

MIDDLEWARES

nerd_jfpb
nerdjfpb nerdjfpb

Middleware functions are functions


that have access to the request
object (req), the response object
(res), and the next middleware
function in the application’s
request-response cycle.
The next middleware function is
commonly denoted by a variable
named next. (official document)

nerd_jfpb
nerdjfpb nerdjfpb

Do you understand anything of it ?

nerd_jfpb
nerdjfpb nerdjfpb

In short,
normally we use middleware
to checking something.
Like -
if anyone is a authentic user
or is the user is admin.

nerd_jfpb
nerdjfpb nerdjfpb

Middleware functions can perform


the following tasks:
- Execute any code.
- Make changes to the request and
the response objects.
- End the request-response cycle.
- Call the next middleware function
in the stack.

nerd_jfpb
nerdjfpb nerdjfpb

An Express application can use


the following types of middleware:
Application-level middleware
Router-level middleware
Error-handling middleware
Built-in middleware
Third-party middleware

nerd_jfpb
nerdjfpb nerdjfpb

You can check the more details -


https://expressjs.com/en/guide/using-middleware.html

nerd_jfpb
nerdjfpb nerdjfpb

Let's use a middleware

nerd_jfpb
nerdjfpb nerdjfpb

We have 3 elements here - request, response


and next cycle. In the middleware we just
console log the time. So when we'll call the
route, this function will be called always.

nerd_jfpb
nerdjfpb nerdjfpb

SEE THE RESULT WHEN WE HIT


THE URL -

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-29

SQL DATABASE
WITH
NODE JS
nerd_jfpb
nerdjfpb nerdjfpb

Before coming this part of tutorial,


we never talked about the database.
Actually it was a basic course so
I never tell anything about
the database.
Just wanted to make pure
node js tutorial.

nerd_jfpb
nerdjfpb nerdjfpb

Today we're going to learn


how can we add
a sql database
with
express js.

nerd_jfpb
nerdjfpb nerdjfpb

It's pretty easy,


but we need a npm package for it.
Actually multiple package for it.

nerd_jfpb
nerdjfpb nerdjfpb

For the sql connection.


There are some packages
Knex.js
Sqorn
squel
Objection.js
and many more

nerd_jfpb
nerdjfpb nerdjfpb

I personally used knex.js


and objection.js.
Objection actually based on knex,
it has some extra benfits
so I used it some projects.

nerd_jfpb
nerdjfpb nerdjfpb

Knex is good choice,


beacause it has an
amazing documentation.
Which is pretty easy to follow.

nerd_jfpb
nerdjfpb nerdjfpb

Installing is pretty easy. As like others. But


you need to install the database too. Like
what you are using - mysql or postgresql or
sqlite

nerd_jfpb
nerdjfpb nerdjfpb

Connecting is easy too,


just changing the credientials
will be okay .

nerd_jfpb
nerdjfpb nerdjfpb

You can write RAW sql with the


knex too.
http://knexjs.org/#Raw-Expressions

nerd_jfpb
nerdjfpb nerdjfpb

Learn Node JS
in 30 Days

Day-30

NO-SQL DATABASE
WITH NODE JS

nerd_jfpb
nerdjfpb nerdjfpb

In this last day of tutorial,


we are going to
talk about how we can
integrate an nosql database
with the node js.

nerd_jfpb
nerdjfpb nerdjfpb

If we use mongodb
as a no-sql database,
which is really really popular.
We can use a package
call mongoose

nerd_jfpb
nerdjfpb nerdjfpb

Check out the mongoose official


document
https://mongoosejs.com/

nerd_jfpb
nerdjfpb nerdjfpb

Installing is pretty easy.


Just putting `npm install mongoose`
will done the work.
You can write `npm i mongoose` too.

nerd_jfpb
nerdjfpb nerdjfpb

After requiring the mongoose


using
`const mongoose = require('mongoose')`
we just need to put
the database name on
the connection

nerd_jfpb
nerdjfpb nerdjfpb

Like this

nerd_jfpb
nerdjfpb nerdjfpb

We can easily find the schema


in the mongoose official page.

nerd_jfpb
nerdjfpb nerdjfpb

In the time on making live project,


you can use mlab for the hosting
mongodb.

nerd_jfpb
nerdjfpb nerdjfpb

So ARE YOU CONFIDENT ENOUGH


that you can read & understand
some node js code right now ?

nerd_jfpb

You might also like