web-dev_javascript
web-dev_javascript
Variables
Variables are containers of data which we can access anywhere in the program
Value Types
String Methods
Arrays
.slice(x,y) Gets a part of the array x - index of the start item y - count of the last item
.sort((a,b) => a-b) sorts the array items of numbers in ascending order
.sort((a,b) => b-a) sorts the array items of numbers in decending order
Node Modules
NPM
Dotenv Module It is a module used to get access to the environment variables from the .env file
npm install dotenv
Javascript Fetch
function callAPI() {
async function getData() {
let response = await fetch(url, options);
let data = response.json();
return data;
}
getData().then(res => {
console.log(res);
})
}
Using APIs
callback(res) {
let resData = ""
res.on("data", (chuncks) =>
resData += chunks
})
res.on("end", () =>
console.log("The data from the API: " + JSON.parse(resData))
})
}
URL Path
"/root/:id" refers to a url with the value in place of id will be stored in a object
Functions
A Function is a small block of code which performs a specific task. We can create a new Function using function keyword.
function myFunc() { // code to be executed }
The code inside the function can be executed by calling the function anywhere in the program.
Parameters are the variables defined with the function, but their values are assigned when they are invoked(called).
myfunc(4)
The return statement stops the execution of a function and returns a value.
function myFunc(parameters) { return VALUES }
conditional statements
Conditional statements are used to perform different actions based on different conditions.
There are there types of conditional statements
if (condition1) {
// code to be executed if the condition1 is true
} else if (condition2) {
// code to be executed if the condition1 is true
} else if (condition3) {
// code to be executed if the condition1 is true
} else {
// code to be executed if all the conditions are false
}
NodeJS
Mongoose
Mongoose is an Object Document Mapper (ODM) that is mostly used when the Node.JS server is integrated with the MongoDB database
Requiring Mongoose:
const mongoose = require("mongoose");
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
Schema and Model:
const Schema = mongoose.Schema(Fields and their info)
const model = mongoose.model("collectionName", Schema)
Using the model variable, we can perform all the CRUD Operations
EJS is a simple templating language that lets you generate HTML markup with plain JavaScript, where we can substitute values from JavaScript
Setting EJS
app.set("view engine", "ejs");
Using EJS
res.render("fileName", {});
EJS Website
bcrypt
Passport is authentication middleware for Node.js. Extremely flexible and modular, Passport can be unobtrusively dropped in to any Express-
based web application
Using Passport.js, Express-Session and as well as passport-local as a strategy we can login a user and maintain the login user
Note
Dotenv
Dotenv Module It is a module used to get access to the environment variables from the .env file
Installation:
npm install dotenv
Configuration of Dotenv
require("dotenv").config()