\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Web Dev} \pdfinfo{ /Title (javascript.pdf) /Creator (Cheatography) /Author (Web Dev) /Subject (JavaScript Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{2A8CBD} \definecolor{LightBackground}{HTML}{F1F7FA} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{JavaScript Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Web Dev} via \textcolor{DarkBackground}{\uline{cheatography.com/186440/cs/38966/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Web Dev \\ \uline{cheatography.com/web-dev} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 28th May, 2023.\\ Updated 9th December, 2023.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Variables}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Variables are containers of data which we can access anywhere in the program} \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.5905 cm} x{14.6795 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Value Types}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Number} & Numeric Values \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{String} & A sequence of characters enclosed within quotes \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{Boolean} & True or False \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} Array & A special variable, which can hold more than one value. \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{Object} & {\emph{Key}} and {\emph{Value}} pairs \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.5264 cm} x{11.7436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{String Methods}} \tn % Row 0 \SetRowColor{LightBackground} `.length` & returns the number of characters \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{`string.slice(x},y)` & returns a part of the string. \{\{nl\}\} {\bf{ x - index of the start item \{\{nl\}\} y - count of the last item}} \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} `Number({\emph{String}})` & converts the string containing numeric values into Number datatype \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{`.toUpperCase()`} & converts the string to uppercase \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{`.toLowerCase()`} & converts the string to lowercase \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.8718 cm} x{11.3982 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Arrays}} \tn % Row 0 \SetRowColor{LightBackground} Declaring Array & `let arrayName = {[}{]}` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Accessing Items & `arrayName{[}{\emph{index}}{]}` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `.length` & returns the length of the array \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `.push({\emph{data}})` & Adds a new item to the end of the array `arrayName.push("data")` \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} `.pop()` & Removes the last item `arrayname.pop()` \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} `.unshift({\emph{data}})` & Adds a new item to start of the array `arrayName.unshift({\emph{data}})` \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} `.shift()` & Removes the first item of the array `arrayName.shift()` \tn % Row Count 17 (+ 3) % Row 7 \SetRowColor{white} `.join({\emph{seperator}})` & Converts the array into string with the seperator \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} `.slice(x,y)` & Gets a part of the array {\bf{{\emph{x - index of the start item}}}} {\bf{{\emph{ y - count of the last item }}}} \tn % Row Count 23 (+ 4) % Row 9 \SetRowColor{white} `.reverse()` & reverse the array items `arrayName.reverse()` \tn % Row Count 25 (+ 2) % Row 10 \SetRowColor{LightBackground} `.sort()` & sorts the array items consisting only strings \tn % Row Count 27 (+ 2) % Row 11 \SetRowColor{white} `.sort((a,b) =\textgreater{} a-b)` & sorts the array items of numbers in ascending order \tn % Row Count 29 (+ 2) % Row 12 \SetRowColor{LightBackground} `.sort((a,b) =\textgreater{} b-a)` & sorts the array items of numbers in decending order \tn % Row Count 31 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Node Modules}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Node modules provide a way to re-use code in your Node application. \newline % Row Count 2 (+ 2) There are internal modules which come along with nodejs and also external modules which we can install using a package manager i.e npm, yarn% Row Count 5 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.5264 cm} x{11.7436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{NPM}} \tn % Row 0 \SetRowColor{LightBackground} NPM & NPM is a package manager for Node.js packages, or modules. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{Initializing} NPM & `npm init -y` \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} Installing a module & `npm install {\emph{moduleName}}` \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} using the modules & `const module = \seqsplit{require("NPMModuleName")`} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.7353 cm} x{10.5347 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{NodeJS Basic App}} \tn % Row 0 \SetRowColor{LightBackground} Importing Express & `const express = require("express");` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Invoking Express & `const app = express();` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} express urlencoding & `app.use(express.urlencoded(\{extended: false\}));` \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} using express's urlencoded & `req.body` \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} Setting EJS & `app.set("view engine", "ejs");` \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} Using EJS & `res.render("fileName", \{\});` \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} Dotenv Module & It is a module used to get access to the environment variables from the .env file\{\{nl\}\}`npm install dotenv` \tn % Row Count 18 (+ 5) % Row 7 \SetRowColor{white} configuration of Dotenv & \seqsplit{`require("dotenv").config()`} \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Basic Mongoose Connection}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{const mongoose = require("mongoose"); \newline \newline mongoose.connect({\emph{connection string}}, \{useNewUrlParser: true\}); \newline const Schema = new mongoose.Schema(\{ \newline field1: \{ type: String, required: true \}, \newline field2: \{ type: Number, required: true \} \newline \}) \newline \newline module.exports = mongoose.mondel( {\emph{collection name}}, Schema )} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Basic NodeJS App}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{const express = require("express"); \newline const app = express(); \newline const bodyParser = require("body-parser"); \newline app.use(bodyParser.urlencoded(\{ exteded : true \}); \newline app.use(express.static("public"); {\emph{ // making a folder to have static files like css, js}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Javascript Fetch}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{function callAPI() \{ \newline async function getData() \{ \newline let response = await fetch(url, options); \newline let data = response.json(); \newline return data; \newline \} \newline getData().then(res =\textgreater{} \{ \newline console.log(res); \newline \}) \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Using APIs}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{`import https from "https"`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{`https.request({\emph{url}}, options, callback() \{\})`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{`callback(res) \{\{\{nl\}\} let resData = ""\{\{nl\}\} res.on("data", (chuncks) =\textgreater{} \{\{nl\}\} resData += chunks\{\{nl\}\} \})\{\{nl\}\} res.on("end", () =\textgreater{} \{\{nl\}\} console.log("The data from the API: " + JSON.parse(resData))\{\{nl\}\} \})\{\{nl\}\}\}`} \tn % Row Count 8 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.0445 cm} x{11.2255 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{URL Path}} \tn % Row 0 \SetRowColor{LightBackground} `"/root/:id"` & refers to a url with the value in place of id will be stored in a object \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{`req.body.params.id`} & access the parameter of the URL Path \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{`req.body.query.pName`} & used to access the path parameters \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A Function is a small block of code which performs a specific task. We can create a new Function using {\bf{`function`}} keyword. \{\{nl\}\} `function myFunc() \{ // code to be executed \}`} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{The code inside the function can be executed by calling the function anywhere in the program. \{\{nl\}\}} \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Parameters are the variables defined with the function, but their values are assigned when they are invoked(called).\{\{nl\}\} `myfunc(4)`} \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{The return statement stops the execution of a function and returns a value. \{\{nl\}\} `function myFunc(parameters) \{ return {\emph{VALUES}} \}`} \tn % Row Count 12 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{conditional statements}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Conditional statements are used to perform different actions based on different conditions.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{There are there types of conditional statements} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{`if (condition1) \{ \{\{nl\}\} // code to be executed if the condition1 is true \{\{nl\}\} \} else if (condition2) \{ \{\{nl\}\} // code to be executed if the condition1 is true \{\{nl\}\} \} else if (condition3) \{ \{\{nl\}\} // code to be executed if the condition1 is true \{\{nl\}\} \} else \{ \{\{nl\}\} // code to be executed if all the conditions are false \{\{nl\}\} \}`} \tn % Row Count 10 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.7715 cm} x{9.4985 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Finding HTML Elments}} \tn % Row 0 \SetRowColor{LightBackground} `document` & The whole html document \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `getElemen\{\{nobreak\}\}tById({\emph{ID}})` & Gets the element with the specified ID \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `getElementByClassName({\emph{class}})` & Gets the elements with the Specified CLASS \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `getElementByTagName({\emph{Tag}})` & Gets the specified Tag elements \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `querySelector({\emph{Query}})` & Returns the first element which satisfy the query \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} `querySelectorAll({\emph{Query}})` & Returns all the elements which satisfy the query \tn % Row Count 14 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{NodeJS}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{NodeJS is a open-source cross-platform runtime runtime environment for backend JavaScript} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\{\{link="https://nodejs.org/en"\}\}Install NodeJS\{\{/link\}\}} \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Mongoose}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Mongoose}} is an Object Document Mapper (ODM) that is mostly used when the Node.JS server is integrated with the MongoDB database} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Requiring Mongoose: \{\{nl\}\}`const mongoose = require("mongoose");`} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Connecting to MongoDB Database: \{\{nl\}\}`mongoose.connect({\emph{connection string}}, \{useNewUrlParser: true\})`} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{A Schema in mongoDB or mongoose is a structure for the data validation. mongoose will return a error if the data is not the same} \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Schema and Model: \{\{nl\}\}`const Schema = mongoose.Schema({\emph{Fields and their info}}) \{\{nl\}\}const model = \seqsplit{mongoose.model("collectionName"}, Schema)`} \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Using the model variable, we can perform all the CRUD Operations} \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{EJS (Embedded JavaScript templating)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{EJS is a simple templating language that lets you generate HTML markup with plain JavaScript, where we can substitute values from JavaScript} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Setting EJS\{\{nl\}\}`app.set("view engine", "ejs");`} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Using EJS \{\{nl\}\}`res.render("fileName", \{\});`} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\{\{link="https://ejs.co/"\}\}EJS Website\{\{/link\}\}} \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.0807 cm} x{10.1893 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{bcrypt}} \tn % Row 0 \SetRowColor{LightBackground} Module & It is used for salting and hashing the passwords \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} installing bcrypt & `npm install bcrypt` \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} requiring bcrypt & `const bcrypr = require("bcrypt")` \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} hashing password & `bcrypt.hash({\emph{"password"}}, {\emph{hashing rounds}}).then(hash = \{ //code \})` \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} comparing password with hash & `bcrypt.compare({\emph{"password"}}, {\emph{hash}}).then(result =\textgreater{} \{\})` \tn % Row Count 13 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Passport.js and Express-Session}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Passport is authentication middleware for Node.js. Extremely flexible and modular, Passport can be unobtrusively dropped in to any Express-based web application} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Using Passport.js, Express-Session and as well as passport-local as a strategy we can login a user and maintain the login user}}} \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Bacis Passport.js, Express-Session Code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{const app = require("express"); \newline const router = express.Router(); \newline \newline const localStrategy = \seqsplit{require("mongoose-local");} \newline const passport = require("passport"); \newline const session = \seqsplit{require("express-session");} \newline const MongoStore = require("connect-mongo") \newline const users = \seqsplit{require("./models/mongo-user.js")} \newline router.use(session(\{ \newline secret: "some secret", \newline store: \{ MongoStore.create(\{ \newline mongoUrl: {\emph{MongoDB Connection String}}, \newline touchAfter: 24 {\emph{ 3600 \newline \}, \newline cookie: \{ \newline maxAge: 14 }} 24 {\emph{ 60 }} 60 * 1000 \newline \}, \newline resave: false, \newline saveUninitialized: true \newline \}) \newline router.use(passport.initialize()); \newline router.use(passport.session()); \newline \newline passport.serializeUser( \newline (user, done) =\textgreater{} \{ \newline done(null, user); \newline \} \newline ) \newline \newline passport.deserializeUser( \newline (user, null) =\textgreater{} \{ \newline users.find(\{\_id: user.\_id\}).then(data =\textgreater{} \{ \newline done(null, data); \newline \} \newline \} \newline ) \newline \newline passport.use( new localStrategy( \newline (username, password, done) =\textgreater{} \{ \newline users.find(\{username: username\}).then( data =\textgreater{} \{ \newline if (data.password == password) \{ \newline done(null, data); \newline else if (data.password != password) \{ \newline done(null, false); \newline \} \newline \} \newline \} \newline ) \newline \newline router.post("/login", (req, res, next) =\textgreater{} \{ \newline if (req.isAuthenticated == true) \{ res.redirect("/") \} \newline else if ( req.isUnauthenticated == true) \{ return next(); \} \newline )} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Note}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Add `type`key to the {\bf{package.json}} to use `import` method to import modules% Row Count 2 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Dotenv}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Dotenv Module It is a module used to get access to the environment variables from the .env file} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Installation: \{\{nl\}\}`npm install dotenv`} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Configuration of Dotenv \{\{nl\}\}`require("dotenv").config()`} \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}