Transcript for:
Node.js and Express.js Comprehensive Lecture Notes

hello everyone welcome to back first point what is noj noj open source cross platform JavaScript runtime environment Environ envir language runtime environment back end part ands V8 and Java user interactivity user to user CH Dynamic applications Dynamic application velopment process authentication and authorization us authentication and application security es6 andt Arrow functions call back functions restructuring asynchronous promises already JavaScript and and next and inst Windows CDE V Space hyphen V npm node package manager node package manag nexts expresss framework expresss concept all the best take carep R stands for read e for eval P for print L for Loop first and basic point so 2 2 is not 2 a 55 65 12as C command line interface so top and topic CLA C command line folder key node tutorial and next St jaas hell s function number two arguments 150 comma 250 two terminal clear node space sample dot topic buin modes important and modules OS module path module FS module HTTP module so first moderating system St Sy mod window node space sample enter hit [Music] Windows C P CP EX system underscore Dr name andore file name variables current folder and files out ins lore drore F terminal SLE tutorial Tut Tut centory andr and next PATH mod so requ statement mod next current directory module key path do D name sample only base name meore file name output node sample only SLE modore fde SLE name and topic FS module previous topics modu and mod Vari first of all text F content like hello everyone welcome to my channel simp firstent and second argument enod thirdd argument function text txt next argument encoder and parameter error second parameter cons cons log error in so cons dat all andde and IND outut hello everyone welcome to my Chex in case welcome to my channel hello everyone my name is Kal my name ISS mod read f for mod and second utf8 in if error outut so conso log error ER log uh file created successfully output index and so all so comment FS do WR file example HTML second content sample and variable uh my name is Kalan I am creating dyamic content content function log error and else BL else f um content is success simple Tex indle rename method so com rame F new changed file javascri so next process so El BL modified successfully modified sucess and next F FS module key FS do unlink new changed file JS F second argument function in if log error console.log file deleted success save chasing put inder create read update delete and and operations modes with examp topic HTTP modp Hy transfer like Compu mobile softw communication http liketh framework frame framewor HTP hptp module create so HTTP do create server function function request response twoam my meod my server len5 [Music] to nodejs tutorial learning response index 5,500 so Local Host 5500 welcome tode welcome to server resp welcome Toc [Music] index client server model action per and topic local modules so first function each number param second function subtract number subract number function a minus B third function divide number and fourth function multiply number so function mod export modes add number comma subtract number number divide number and next multiply [Music] number requ parenthesis slash demo mod 11 mea modules second one subtract numbers mod module divide number divide number argument 10 comma 2 multiply number the multiply number 10 comma 5 index so IND 21 5550 number 21 outut subtract number module and number hello everyone welcome Tob tutori frame popular nodejs web application framework features and Concepts expresss it all npm unit npm Space Unit E command environment [Music] setage Jon so file Lo example console. log welcome welcome to Xpress welcome third partages dep l details and installation commands and version details okay and so script first of all start key start keyue noon space re or C command welcome everyone Express tutorial SLI topic creating a web server easy and important topic command npm init Hy by I first of all so Express Pages inst start and application okay and Next Step app.js file first of all require Express already packages modes port number [Music] call function call function output cons Lo cons. log server started and running successfully any terminal npm start started and running [Music] success second function respon first parameter request second parameter response Ser example apple is red in color and R save [Music] [Music] j000 is welcome everyone expr top middle middle midd request process satisf l for example f number inom number middle midd and and middle last number npm start Local Host number 5000 so Local Host 5,000 HC slash home I amage this user SL1 121 user11 you search for user1 only app three arguments first one request second one response third one next next without interruption without any interruption [Music] [Music] NK ref home first Handler and next first Handler is great next function l second middleware key second Handler third middleware key third Handler second midd next function element PR sorry you are not allowed is less than second us thir so resp sorry are not so middle request forward user1 l resp gu so next next coming videos AP concept welcome everyone node expresss tutorials connection Rel datb nol database ml datab structured query language relational databases structur databases structured databas dat tables rows and columns system and delete comp dat welcome to what is your explore bu what type of application you building I'm just exploring what is your preferred language JavaScript Javas and next [Music] cluster project option us and password ABCD 3 4w 0. terminal npm init Hy y First Express node one andv first npm install expresson andv and inst and sec con password password password c 1 2 3 4 password Server create so server first of allpress con Express require Express con enpr Express con Port variable key 5,000 Port call function call function template string server started and running at template string SC T start key key start and Main F server npm start hit and running successfully started and running at 5000 Mong ENT M start Cent so client is equal to require mongod DB process process glob cons less process variable config parthis Proco client. connect e method key arguments first argument process EnV dot Mur function function statement console. log connection DB connected successfully catch block error key so error INE error error comma error save ch for example password password AB CD 1 2 3 2 3ig connection application M datab operations so next operations andaction welcome everyone so what AP AP model view controller what API API application programming interface rules and differentt and and back back and appliations in rest modern web and mobile applications above 95% rest so transfer service operations C for create R for read U for update D for delete operations perp methods get post put delete post dat put method dat delete me datp and next point model view controller follow model view controller so F environment variable f already Express mongod so npm install first Mong Mong m datab okay npm install mongus body and body Express Express variable Vari to body parser body parser require propess EnV process. global object already last topic discuss and next port number port number argument pass sorry port capital templ string server started and running at [Music] variables app conne EnV do config config connect method ke process. unv Global variable key already connection _ Ur variable m block connection ins. log connection and Su mongod connected successfully message successfully cat eror L back eror next con examp AP so model employee and employ employ sing employe employ employee controller and next Mong require Mong employee new mongus scha dats the employee name employee email and employee phone and employe City prop number required e ke value the true Ean Value Pass say Emil validation key required true valid next property phone data type number isone number def Val property next property city city property string validation export module exports equal to mongos do mongos model model essorts next employees controller contr emplo and create employee function function request and response first name email phone and City properties reest object properties name email phone so responsea status red IND 400 ER 500 ER 400 console log conso log there is an error so message server error controller function Soom module exports create employee F create F employee routes JavaScript file so first fpr Express require Express router rpr method Express router const employee controller is equal to require employe controll controll employ empe models folder employee get method the post put or patch method delete rou router Post mod already Hy employee second controller function controller function create employ employe controll employe controller already create employe do exports is equal to router so employee rout requ statement employes employe sing open anduse appuse employe routes po Sy Local Host Ur employ and end point add Hy EMP so post first of all call method post method the post method a local host colon 5,000 slash employees slash add hyen EMP end and next Dron and next body name Emil phone City First Property name property double name property name property string datae email key string data typ phone key next prop email gmail.com next property phone next to city data type city property and then keep data type string [Music] double F already body body parser meod body parer left section datab database okay browse collection EMP employ.com so for example Sur at gmail.com and phone number place 12121212 number c so email gmail.com phone number 3636 36 36 and SRA thes datab first record second record third record model controller get put and delete welcome back everyone restful AP topic continuation video so last video operations read delete conect datab con so last top V and email VJ at gmail.com phone number 4 4 4 City okay for example Mah and mahes at gmail.com phone number 18888 and the Citys EMP get employees function so syn request response parameters AR function body emplo so employee model response status 200 errorr block console dot error error key statement there is an error column error status Cod response. status 500 500 message col server error already mod export create employee get employees get method router. get method end all employees emplo employ controller get employee 5000 browser Local Host col 5,000 [Music] and employes employe rout all employes Point all employees mahes datab sorry datab numberand City second dat single employee to function the function a sync and parameters request response Arrow function body prom con employee variable Vari key employee model by idid ID meod request do ID request params ID find by ID response status stat if not employee return response. status 44 and record not found Json object L message message employee not found St cat block er error key there is an error statement actual error error response status status 500 Cod pass format object object message col server error single employee next employe and point employee SL colon ID pass employee controller do single employe ID browser all employees Place employee colon employee slash idopy ID thir get and post meth next method put or P function update employee and response function of properties name email phone and City so properties body properties First Property name email phone and City request. body body proper my employees my employee employe ID and update reest name property response status employe employe if statement if no my employee my employe statement if return response. status object message colum employee notor cons error there is error response status 500 server error Jon form object message colon server error update employee employe R router do put put method and okay end update emps reest employ employee controller employe controller dot function per update employee so to empy employ employee details employee and points first of all m f server.js end update SL colon ID update slash for example datab email goam at gmail.com and only and sindabad Place City why name and City next delete meete delete con delete employee function this the function Asing function so request response to parameters delete employee lock variable the variable key a wait word employe model employee do me find by ID and delete respon stus 204 send method next catch block eror comma enter h we delete delete delete endend delete SL col ID employ delete so employe function employe delete delete employe function employe controller delete employe delete URL end points delete ID 20 so D Del respon Del okay get post put and delete rest get post put delete me op hello everyone welcome back node Express tutorial topic API integration and last so first of all last [Music] contact employ Ur end 5000 number all employees so all hyph employee employes all employ HP second third m real time react application employee four four four properties name phone and City properties comp Ur EMP application end point add EMP SOI head Serv very main component component create add EMP to comp field name input field gmail.com phone number 4745 6213 random number [Music] EMP all emplo name and Sumit at gmail.com phone number random number City so app sucess us and [Music] passw first usern name email and password user Lage us email and password submit l application hello everyone welcome back node Express tutorial auth soth what is authentication authen sessions and auth point and what is authentication authentication user identity confir example Bank application social media accounts medical applications soth and authentications state authentication stess authentication State user data State Authentication stat authentication tokens stth State authentic session and user specific data user details like user ID login status per session and next cookies wex and re s authentication s us ustic so first of all practical auth server. M request response parameters AR function l [Music] response object in the fruit key fruit key value mango client side rendering side rering okay loal host 5000 slash cre mang and Local Host Local Host 5000 [Music] mango [Music] add HT for server s rendering for example gra reest response AR function this H1 this is grapes Local Host 5,000 slash grapes so rering and centering difference [Music] template template ejs e template HTML PES with styling npm install ejs ENT template engine application inst e app.use method Key View engineer last topics model and controller follow MC model view controller model view controller model controller view folder F sample page exclamation mark HTML like H1 welcome to my page main file server response [Music] ER okay Gra request First Express inston body indne conne F indexs connection and connection server creation to welcome. index expra f [Music] respon application Local Host 000 8000 sign uput field username second email third password subm button Local Host lout passw dashboard Express session stateful authentication started and running Mong connected successfully appc install Express sessions F Express session [Music] IND session session variable key Express session [Music] app. use session session First Property secret this is Secret reive property false value save uninitialized uninitialized cookies cookies connect mongod session stage impl [Music] npm command [Music] documentation line copy [Music] connect MB session so close index M connection store Vari variable mongodb Storey store create new keyword process. EMV do uncore my session session cre project rest so c pro STM start start session and password model user require just con mongus is equal to require mongus so last previous topics model view controller variable user schema variable instance new mongus do schema username email and password so let username then type string export module do exports equal to mongos do model model user user scha user user schema scha and next index user equal requ model SL models SL user requ okay fer register and password method post method Express dot URL encoded object exted true [Music] function function function function request response param properties username so const username email the password reest variable user us model username email and [Music] password keyb new user cat BL catch BL error error body console error response redirect page sign sign up response do redirect to login page redirect so SL lows page user details first usern so email Kalan gmail.com and password random password us sign and uh application user is not a Constructor line number 53 53 user model user schema user import ch okay export sorry exports application refresh IND application start rest application restart email Kalan gmail.com password random password USS collection register user details and email and password okay I'm very sorry s [Music] very sorry register sign usern usmail.com password okay s second [Music] s authentication process user details provide server details USS USS user IND session and email Mahi at gmail.com password redirect Rec user records user collection [Music] refresh refresh refresh s latest latest session user details datab so function request session us details for a new user do username sign up for example buy at test.com p sessions sessions collection refresh latest us then know Local Host 800 test.com password user register first record B second record Harish s sessions created last session sample second session Harish first session bti individ authentication steps follow authentication program so first session user [Music] identity l USPS command copy application so V const bcrypt is equal to decrypt okay password poister register route function function function parameters request response Asing function US password properties DCT request useri a wait user model find [Music] one US user register page redirect response. redirect slash register [Music] sign sign up and next US password hash password variable ke a wait bcrypt password password second argument user equal to new user model user variable model properties username Emil and password password password wait us userve user sucessful user resp. redirect l pect l so sign up page usern name usmail.com password password submit users records okay seven records us L passw password user L [Music] post slash user iphon login function request response parameters function function and password only password user variable wait user find one find one in case uh not user user redirect now response do SL redirect to sign up page and next user password con check password wait password database US pass comp password Vari password user return user sign up page redirect redirect to sign up us email and password correct welcome page response redirect dashboard authentic dat s us alls Windows close first Local Host is sign up username email Cod Su gmail.com password SU Lo [Music] page session refesh okay okay Su gmail.com password SU Local Host [Music] sessions user deta request session user user authenticate request session in key is a authenticate is authenticated True Value okay variable check authentication check variable request response next next function already topic midd request. session do [Music] is is authentication is authenticated session in case the request do session dot is authenticated and the true next function okay sorry in case if condition satisf user redirect toing redirect us sign Che without sign up without autic Chanel mahes at gmail.com email Mahesh gmail.com password Mah authentication datab Rec users record s session session session person Mah and is authenticated true session browser browser Local Host col 8,000 slash sign up gmail.com password submit pect login key email Sunil at gmail.com password Sunil sub dat so session okay second okay user examp log form app do Post method key log out out function function key AR function this one request response request session destroy us M ref cannot be reached application restart okay refh okay Su sorry refresh refresh sessions stage s okay sa uninitialized false delete username Su us and email sum gmail.com password Sumit subm Lo pect email sum gmail.com passord datab sessions page sessions collections session collection refres usmail pass datab my sessions collection Lo e page M email Sumit gmail.com password applications difference e e different [Music] L page gmail.com password database Loc [Music] Mah local host login page password without authentication so [Music] Lo log page account Mahesh at gmail.com password mahes sub password Mah mahes just sign create Local Host 8,000 user register sign upout sign up is user isar gmail.com password okay and is gmail.com password and US page refresh okayo everyone welcome back node Express tutorial s on topic JWT tokens so last top auth twoth auth authentications Tok first of all body Mong and 4000 all USS so tokens inst jsonb Tok is equal to require Json web token in so of all us us first object id id key one username usern name Mah username mahes password object ID to Chanel the username password users log and log out through JWT tokens two users only just user so admin post method API SL login route key request response function body first of all username and password so const username password is equal to request. body and Next Step low variable user variable find e method function function user person variable return person do username is equal to actual username like person do password is equal to actual password username and password next user details user details Json user else in case response. status 4 1 Json user credentials not matched and see post method application Local Host 4,000 slash API AP l SL API SL login SL API SL Lin body Jon format raon body first of all usern name mahes password soer details user details usern name Mah Comm password details okay okay okay sorry app. use express. Json configure J Express just user details okay US US password Cod Sur could send username password username and password access token dets user is secret USS and user is admin admin if I my secret key my name my name [Music] is this secret key process. secr passw secret second key format l username user do username and is admin user do is admin ACC Tok username password sorry is admin secret key equal to already page so re con do equal to require config ENB dot config Next Step flow okay my process secret [Music] key post man usern and [Music] or look sorry ID false ID and for example username username username next comma save just say Tok username is false dets mahes account mahes passw copy J JWT [Music] ad toal application auth only only user Mah m app do delete AP user user SL colon ID [Music] authentic author authorization value bar space [Music] [Music] con verify user request parameters slow request response next [Music] function user token is equal to request do headers do authorization if Lo if and else if Lo condition user [Music] Tok user token split to spit user token next argument secet secret ke call back function call back function error and user two parameters call response status 43 error error token is not valid and next if if condition fail request user is equal to actual user nexton status 401n error name you are not authenticated next verify user call back function L request response Arrow function delete user L user so request user do ID equal to equal to request. params user ID user request do user do is admin response status success status 200 yes user is deleted successfully Lai in response status you are not allowed to delete delete M ID number ID delete method application Local Host 4,000 and user ID present one next St delete head authorization authorization bar space toen s not us Tok token log Lo password delete me token place token place send just you are not allowed to delete only not allowed to delete you are not allowed to delete deleted sucess user log and [Music] logm install AAL to requ AES config set View engine second argument ejs L and [Music] USS ur ur encoded object L extended true fold files first log file l. ejs log out so then welcome. ejs first welcomeing page HTML docent welcome page welcome to my application simple text which I am I I amage I I am s letage action API log us button key log out and button type submit get method app.get first login key AP SL log Lo ke yes AP L get me US function key request response Arrow function body l con user id id request. fors user ID user ID equal to equal to one only one response. redirect redirect to in case Sur ID El if user ID equal to equal to 2 username redirect to Sur in else block low response status error 500 Jon 500 43 for Jon user not found Lo us not app.get mahes route hit open page request response Arrow function response render Maher so application Local Host col 4,000 and uh API SL login slash for example one and Mah Local Host 4000 Mah default no default engine was specified and no extension was provided problem mahes key direct go okay okay I am for example Local Host colon 4,000 SL API login1 okay route case Local Host col 4,000 slash API slash login slash2 us us Lo app. poost codes SL rout API SL logout call back function call back function request response Arrow function body block if user token us token statement to con index all tokens all tokens tokens index token in case index if not equal to equal to minus1 minus one indic so if condition key all tokens do splice token index meod token IND Next Step status 200 J logged out successfully elow response. status token not found token not found you are not valid user [Music] response. status 400 Json you are so token not token not found e else block so user tokens and block block authenticated response status 400 Jon you are not authenticated Allens next userc so app get SL API SL logout call back function request response user log socome so. slash homepage homeage reest response welcome pcome okay and next us response redirect homepage [Music] slash Local Host 4000 okaying a/ login mahes uh uh number one okay okay welcome to my application landing page welcome to my application J auth JSM rep tokens B