Back End
Back End
npm init -y after going insided the folder in terminal use this command
const app=express()
app.listen(3000, ()=>{})
create route
get -this takes two paramrter path and callback
call back -A function with parameters (req, res). This middleware function defines
how the server should respond to the request.
app.get('/',(req,res)=>{
res.send("hello");
})
app.post('/',(req,res)=>{
const {name ,brand}=req.body//ismai name and brand pada goga is req ke body mai
res.send("hello");
})
The reason you are not able to get data from the req.body in your POST request
could be due to the absence of middleware to parse the incoming request body
middelware -bodyparser
ye request ko parse karta hai body mai jo data pass hua hai
-----------------------------------------------------------------------------------
--------
file strutre
index.js mai put routes then the routes should some work like business logic so
then put that route logic in controller(yeaha opertaions hota )
1- config folder -in this folder we configure the connection node js and Mongoose
database
model define
const mongoose=require('mongoose')
const todoSchema =new mongoose.Schema({
title:{
type:String,
required:true,
maxLength:10,
},
//like this you can write more object
})
exports.createTodo= async(req,res)=>{
const {titel ,description}=req.body
const response =awiat Todo.create({title,description}
res.status(200).json({
data:response;
})
modulw.exports=route
app.use(express.json())
req.params.id: Use when the ID is part of the URL path (like /users/:id).
req.body.id: Use when the ID is part of the request body (usually with POST, PUT,
PATCH).
post:{
type:mongoose.Schema.Types.ObjectId , //object id of post type
ref: "Post",// refrence to the post model
},
chat gpt
exports.createComment=async(req,res)=>{
try{
//fetch data fro req body
}
catch(error){
}
//find the post by id add the new commnet to its comment array
new true mean -jab ye sara kaam hojye jo upara likha hai tab jo updated doc hoga
wo return kar na
Class -5
const express=require('express')
const app=express()/application is created not live but created
middle ware--->before sending resposnse if you want to do somthing then middle ware
is used
-----------------------------------------------------------------------------------
-------------------------------------------------------
AURTH N And AUTH Z Class 1
req ke header mai se authorization key se value le au aur jaha bearre ko empty
string se repalce kar do
req.cookies.token||
req.body.token||