Skip to content

AnshSinghSonkhia/Learning-Backend-EndGame-2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Backend - EndGame (Part 2) - NodeJS

Learning what matters

Tech-Stack:

Run app

npx nodemon

Flash Messages

Whenever, we want to give some information on EJS Page - Good Looking Alerts, warnings and Descriptions.. It is known as flash messages.

Steps to give Flash Messages:

  1. Make sure, you setup express-session
express project_name --view=ejs

npm i
npm i express-session
  1. Install Package - connect-flash
npm i connect-flash
  • In app.js
const expressSession = require("express-session");
  • In app.js, after app.set('view engine', 'ejs'); and before app.use(logger('dev'));
app.use(expressSession({
  resave: false,
  saveUninitialized: false,
  secret: "kuch bhi string as a secret code"
}))
  1. Make sure you put connect flash in a app.use function. As it runs before running any route.
const flash = require("connect-flash");
  • after app.use(expressSession({...
app.use(flash());
  1. Create flash in any route.
router.get('/failed', function(req, res, next) {
  req.flash("age", 12);
});

router.get('/check', function(req, res, next) {
  req.flash("age");
});
  1. Try to run app.use in any other 2nd route.
router.get('/failed', function(req, res, next) {
  req.flash("age", 12);
  req.flash("name", "Ansh Singh Sonkhia");
  res.send("Bangayaa");
});

router.get('/check', function(req, res, next) {
  console.log(req.flash("age"), req.flash("name"));
  res.send("Check kr lo backend terminal pr");
});

You can NOT use FLASH without Express-session.

  • Flash message means to create a data in any route of server and use that data in 2nd route.

  • Using Global Variables is a BAD Wrong practice in this industry. It's so risky and less secure.

  • Flash Messages helps us to use a data created in 1 route in 2nd route.

Use-case application of connect-flash

When user is not able to login, user enters wrong email password. The, we have to forward him to another route. where, we will show him the error message, of wrong email.

But, we can not send data directly from one route to another.

As, using global data is not secure.

Here, flash messages helps us in sending data created in one route to another route.

Intermediate - MongoDB

  • install Mongoose
npm i mongoose
  • Setup Mongoose in users.js
const mongoose = require("mongoose");

mongoose.connect("mongodb://127.0.0.1:27017/testingendgame2");

const userSchema = mongoose.Schema({
  username: String,
  nickname: String,
  description: String,
  categories: {
    type: Array,
    default: []
  },
  datecreated: {
    type: Date,
    default: Date.now()
  },
})

mongoose.model("uesr", userSchema);
  • in index.js
const userModel = require("./users");

How can I perform a case-insensitive search in Mongoose?

Case-insensitive --> whether the user wants to find Ansh or ansh, the result should arrive.

index.js with Case-Sensitive Search

router.get("/create", async function (req, res, next) {
  let userdata = await userModel.create({
    username: "Ansh Singh Sonkhia",
    nickname: "Nimmi",
    description: "A Passonate SDE from INDIA.",
    categories: ['JS', 'TS', 'Tailwind', 'Kotlin'],
  });
  res.send(userdata);
});

router.get("/find", async function(req, res){
  let user = await userModel.find({username: "Ansh Singh Sonkhia"});
  res.send(user);
});

index.js with Case-Insensitive Search

router.get("/create", async function (req, res, next) {
  let userdata = await userModel.create({
    username: "Ansh Singh Sonkhia",
    nickname: "Nimmi",
    description: "A Passonate SDE from INDIA.",
    categories: ['JS', 'TS', 'Tailwind', 'Kotlin'],
  });
  res.send(userdata);
});

router.get("/find", async function(req, res){
  var regex = new RegExp("Ansh Singh Sonkhia", "i")
  let user = await userModel.find({username: regex});
  res.send(user);
});
  • ^ - Shows the start
  • $ - Shows the end
router.get("/find", async function(req, res){
  var regex = new RegExp("^Ansh$", "i")
  let user = await userModel.find({username: regex});
  res.send(user);
});

How do I find documents where an array field contains all of a set of values?

To find all data documents with people with category - JS

router.get("/find", async function(req, res){
  let user = await userModel.find({categories: {$all: ['JS']}});
  res.send(user);
});

How can I search for documents with a specific date range in Mongoose?

  • $gte --> greater than equal to
  • $lte --> less than equal to
router.get("/find", async function(req, res){
  var date1 = new Date('2024-02-14');
  var date2 = new Date('2024-02-16');
  let user = await userModel.find({datecreated: {$gte: date1, $lte: date2}});
  res.send(user);
});

How can I filter documents based on the existence of a field in Mongoose?

All the documents which have field - categories

router.get("/find", async function(req, res){
  let user = await userModel.find({categories: {$exists: true}});
  res.send(user);
});

How can I filter documents based on a specific field's length in Mongoose?

Find all people, within a specific field length

  • $strLenCP --> String Length Compare
router.get("/find", async function(req, res){
  let user = await userModel.find({
    $expr: {
      $and: [
        {$gte: [{$strLenCP: '$nickname'}, 0]},
        {$lte: [{$strLenCP: '$nickname'}, 6]}
      ],
    }
  })
  res.send(user);
  // Array inside another object [{}]
  // We will get All the strings with nickname's length 0 to 6
});

Authentication and Authorization

passportjs - passportjs.org

Follow these Steps

  1. Install these packages:
npm i passport passport-local passport-local-mongoose mongoose express-session
  1. Write app.js code in app.js file --> after view engine and before logger
app.use(expressSession({
  resave: false,
  saveUninitialized: false,
  secret: "kuch bhi string as a secret code"
}))

app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(usersRouter.serializeUser());
passport.deserializeUser(usersRouter.deserializeUser());
  1. In index.js --> write this to require..
const passport = require('passport');
const localStrategy = require("passport-local").Strategy;
passport.use(new localStrategy(userModel.authenticate()));
  1. setup users.js properly
const mongoose = require("mongoose");
const plm = require("passport-local-mongoose");

mongoose.connect("mongodb://127.0.0.1:27017/testingendgame2");

const userSchema = mongoose.Schema({
  username: String,
  nickname: String,
  description: String,
  categories: {
    type: Array,
    default: []
  },
  datecreated: {
    type: Date,
    default: Date.now()
  },
})

userSchema.plugin(plm);

module.exports = mongoose.model("user", userSchema);
  1. In index.js try register first, and then other codes as well

Protected Routes

  • These are routes, which can not be accessed if the user is not login.