Learning what matters
npx nodemon
Whenever, we want to give some information on EJS Page - Good Looking Alerts
, warnings
and Descriptions
.. It is known as flash messages.
- Make sure, you setup
express-session
express project_name --view=ejs
npm i
npm i express-session
- Install Package -
connect-flash
npm i connect-flash
- In
app.js
const expressSession = require("express-session");
- In
app.js
, afterapp.set('view engine', 'ejs');
and beforeapp.use(logger('dev'));
app.use(expressSession({
resave: false,
saveUninitialized: false,
secret: "kuch bhi string as a secret code"
}))
- 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());
- 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");
});
- 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
withoutExpress-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.
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.
- 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");
Case-insensitive
--> whether the user wants to find Ansh
or ansh
, the result should arrive.
index.js
withCase-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
withCase-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);
});
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);
});
$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);
});
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);
});
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
});
passportjs - passportjs.org
- Install these packages:
npm i passport passport-local passport-local-mongoose mongoose express-session
- Write app.js code in
app.js
file --> afterview engine
and beforelogger
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());
- In
index.js
--> write this to require..
const passport = require('passport');
const localStrategy = require("passport-local").Strategy;
passport.use(new localStrategy(userModel.authenticate()));
- 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);
- In
index.js
try register first, and then other codes as well
- These are routes, which can not be accessed if the user is not login.