nodejs introduction

 node js is like a php 

node is a javascript but run on outside browser

node js is completely diffrent from browser js 



to check node version node -v

built on chrome v8 engine

we can crud operation with database

developed by ryan dahl in 2009

it is javascript based frame work

it uses javascript on the server

released under mit license

open source completely free

it is a server side platform

build on google chromes javascript v8 engine

what web server does is take a request from client , proces it , takes from databse and send to client


crud using nodejs


node js is a server side environment


we can develop web applicatiaons, apis using nodejs

node js is afree open source

it can work on any os like linux, mac and windows

]

how to use console for testing node or javasript 




there is no wndow in object because its not run in browesr

nodejs features

it is asychronois and fast driven

means

when server got 2 requests at a time, it will solve and start at a time, didnt wait for completion of firstrequest, bot wil start at a time

very fast

single threaded (only onethread - responds to multiple requiest of the client)

two clients

it takes from one client and responds to taht aclient requiest - if it takes or not takes time

if one client requiest is not complete or complete

it not bother- it takes request from another client it respoids


event driven

higly scalable

no buffering

mit licensed


nodejs uses\\

instala nodejs

from command line also possible

environ ment setup 

got to this website - dowload as per ur os and version

https://nodejs.org/en/download/



it will come with run time environment

react hosted on npm

npm is hosted on npmjs.com

it has pacakges and it consists of folders or modules- all files are in shareable js

no quality control- any body can publish in npm js.com

how to tld i is good- number of weekly downloads

if pacakges has bug, any body can resolve that bug if not fixed

npm --v


and npm- package manager - it has predefined modiles

npm - nodejs pacakage manager

npmjs.com

it has thusand of modules whcih we can use modules

before learning nodejs u have to learn

es6 - callbacks, promises, asyc wait

applications

 chat applications

game server

streaming servers

i/o and apps

i/o bound appss


open file

save as script.js

write some console .log

run usng node script.js

it willl run


what is npm and howto install npm modles


npm view underscore - it will show all the details of underscore module

then if u need to instal

npm installl underscore

with version

npm install underscore@1.4.0 --save

--save - to reflect changes what  u install

npm init-f - package instal for ur installation changes - if package not moified

npm list

to list all the modules

npm init -y (evrything default)

npm init 

after running above command - it will generate package.json file 

in that below text there

iniitial file

name- name of the programm

version - of ur buliding app

description- about ur buildingapp

entry piont

test command -

git respostiroy - url of git

keywords: finding this prokect

author: developer

licesnace:mit


whatever u install taht willl be reflected in pacakge.json


npm outdated


it will show outdated versions in this pacakge.json

to update outdated versions

use npm update --save

it will update all outdated versions


how to instal package

npm unistall expres --save


to know what packages it has

npm ls


to list the packages of what installed in npm it will show that

not locally install gloabally


npm instal express --gloabl

locatuon not shown present folder

it shows from root of ur pc

means u can use from any folder this express


in local list it will not show


npm ls


it is shown in glovbal installation

npm ls --global

installl local and global npm

npm i packagename


global

npm install -g package name

npm i nodemon -D

npm i nodemon -save -dev

this run every dependencies



loadash

booststrap



npm run start

npm run dev - for every save we not run npm run start, if we run dev

nodemon restart for every change in file

similary npm run start command - we have restrat server for every change to reflect in ui



it will automaticlayy runs whenever new thing added

nodemon means watching my applicatiaon


syncrhonus program step by step

 


in above programm

ist import fs

then print prograamm started

then read the file

after reading file

print program ended


asyncronous programm

below

 



in above programm

ist import fs

then print programm started

then read file 

read file function is going on

iwithout waiting for read it will print program ended

but reading file happends when reading file over it will print taht data

as soon as reading file overs- it callbacks and print the text

one function willl not depends on another function to execute

but line by line 



node js call backs
how the actual programm is 





first it go fs.exits with function fileexists
in file exists it go to status
in status it go to read file which is in brackets

event - envokes
when event cames that corespondng function calls
on change or on input or on scroll that is event

event signalts event listenere function to get executed

event emitter raise the event

 it is like addeventlistner- whenver click this evnt raised


emitter.emit - calls event
raised two times
print tw times different response if params there print that
https://nodejs.org/docs/latest-v17.x/api/http.html




in above code 
it imports events
whenever emitter calls events (emitter.emit("my event"))

emitter.on - whenever emitter event raises
it will call 
callback function 
function(){}
then prints event fired



emitter.once means - one time 

multi casting - multiple listenrs called




nodejs template engine -- 

efective java script templating is a template engine
it is librarry for generateing reusable html structur and content

git ejs



we can use this type of 

in diffenrent pages

advantatge s of ejs
simple tempalte tags like <% %>

ejs allows javascript to generate html





npm install ejs

set ejs as template engine

app.set('view engine', 'ejs');

index.ejs

route all requests to the root direcory
github gymnasium courses url https://github.com/gymnasium/GYM-012
app.all('/', function () {

res.render('index');
} );





nodejs cannot handle form data automatically
instll the body-parser package to enable node to handle the form data
require('body-parser')

app.use(body-parser) - means tell to node use this

applications

to do list app



npm install node-persist

super easy perissitent data structures innodejs modeled after html5local storage









difference between package and pacakge lock json

package- it shows installed packages
package-lock.json 
in this all packages dependencies on installed

package.lock.json is created for locking the dependency with the installed version. It will install the exact latest version of that package in your application and save it in package.json. Let’s say if the current version of the package is 1.3.2 then it will save the version with (^) sign. Here carot(^) means, it will support any higher version with major version 1 for eg. 1.2.2. 

Without package.lock.json, there might be some differences in installed versions in different environments. To overcome this problem, package.lock.json is created to have the same results in every environment. It should be in source control with the package.json file because if any other user will clone the project and install dependencies then it will install the exact same dependencies as in package.lock.json to avoid differences.


It is automatically generated for those operations where npm modifies either node_modules tree or package.json


if u install again using npm installin another server

that will install from package.lock json only


https://nodesource.com/blog/the-basics-of-package-json-in-node-js-and-npm/


event loops


https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

https://nodejs.dev/en/about/

https://nodejs.dev/en/learn/understanding-processnexttick/

https://nodejs.dev/en/learn/javascript-asynchronous-programming-and-callbacks/

https://www.youtube.com/watch?v=PNa9OMajw9w
https://www.youtube.com/watch?v=8aGhZQkoFbQ


call back

document.getElementById('button').addEventListener('click', () => { // item clicked })

window.addEventListener('load', () => { // window loaded // do what you want });



javascript is line by line syncronous





asyncronous set timeout




set time out is asyncronous( after 2 secpnmds run this)
setinterval is sychronous

after every 2 seconds run this 



create server - like set interval


 



blocking code
if for lloops in about sections
it will loop
because of this loop
remainong pages not working

means undefined loading
beloow code is blocking because of for loop means its not going to next line
this is draw back of syncrhonous







No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...