#37 API: Handling PATCH Request | Working with Express JS | A Complete NODE JS Course

#37 API: Handling PATCH Request | Working with Express JS | A Complete NODE JS Course

procademy

1 год назад

13,561 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Kristy
Kristy - 26.07.2023 01:21

gold

Ответить
VAKUL SAXENA
VAKUL SAXENA - 12.05.2023 08:53

🎉🎉

Ответить
odai alazzeh
odai alazzeh - 19.03.2023 12:31

Can we use spread operator to handle object modifying instead of Object.assign

Ответить
kishan singh
kishan singh - 09.03.2023 13:45

Deleted req is not working

Ответить
kishan singh
kishan singh - 09.03.2023 13:44

hi sir i am facing problem for post and patch req
/without fs.writefilesync it is genrating response in postman
//with fs.writefilesync it is working completitely fine in vscode also records are being added
//into our movies.json file but giving error in postman
I have also pasted my code

Ответить
kishan singh
kishan singh - 09.03.2023 13:43

const createMovie=(req,res)=>{
console.log(req.body);

const newId=movies[movies.length - 1].id + 1;
const newMovie=Object.assign({id:newId},req.body);
movies.push(newMovie);
//fs.writeFileSync('./data/movies.json',JSON.stringify(movies),(err)=>{
res.status(201).json({
status:"sucesss",
//property defined in our custom middleware
data:{
movie:newMovie
}
})
//})

};

//without fs.writefilesync it is genrating response in postman
//with fs.writefilesync it is working completitely fine in vscode also records are being added
//into our movies.json file but giving error in postman
const updatemovie=(req,res)=>{
const id=req.params.id*1;

let movietoupdate=movies.find(el=>el.id===id);

let index= movies.indexOf(movietoupdate);

let output=Object.assign(movietoupdate,req.body);

movies[index]=movietoupdate;

//fs.writeFileSync('./data/movies.json',JSON.stringify(movies),(err)=>{
//200 fetched sucessfully
res.status(200).json({
status:"sucess",
data:{
updatedmovie:output
}
})
//})
}

/*const deleteemovie=(req,res)=>{
const id=req.params.id*1;

let movietodelete=movies.find(el=>el.id===id);

let index= movies.indexOf(movietodelete);

movies.splice(index,1);

fs.writeFileSync('./data/movies.json',JSON.stringify(movies),(err)=>{
//200 fetched sucessfully
res.status(204).json({
status:"sucess",
data:{
movietodeleted:null
}
})
})
}*/



app.get('/api/v1/movies',getAllmovies);
//name is optional here
app.get('/api/v1/movies/:id/:name?',getmovie);
app.post('/api/v1/movies',createMovie);
app.patch('/api/v1/movies/:id',updatemovie);
//app.delete('/api/v1/movies/:id',deleteemovie);

Ответить