안녕하세요. 강의를 수강하고 있는 학생입니다. 글 주인만 list를 삭제할 수 있는 코드를 작성하다
궁금한 점이 있어 질문 남깁니다.
처음 마이페이지를 만들 때도 로그인이 안 되어있으면 화면상에 창이 띄어지게
app.get('/mypage', 로그인했니, function(요청, 응답){
응답.render('mypage.ejs', { 사용자 : 요청.user })
})
function 로그인했니(요청, 응답, next){
if(요청.user){
next()
}else{
응답.send("<script>if(!confirm('로그인이 안 되어있습니다.로그인하시겠습니까?'))location.href='/';else location.href='/login';</script>");
}
}
위의 코드처럼 응답.send를 처리했습니다. 로그인이 안 되어있으면 예상대로 confirm이 실행됐습니다.
그래서 delete하는 것도 똑같이 작성했습니다.
app.delete('/delete/:id', function(요청, 응답){
console.log('a')
if(요청.user){
console.log('b')
db.collection('post').deleteOne(
{
_id : parseInt(요청.params.id)
},
function(에러, 결과){
console.log('삭제완료');
응답.status(200).send({message : '성공했습니다.'})
})
}else{
console.log('c')
응답.send("<script>if(!confirm('로그인이 안 되어있습니다.로그인하시겠습니까?'))location.href='/';else location.href='/login';</script>");
}
})
코드는 위와 같습니다.
로그인이 안 되어있다고 가정하고 실행시켜보면 console창에 a c 가 잘 출력되는데 confirm은
실행이 안 됩니다. 그래서 응답.send가 필요가 없는 건가 싶어 지워서 작성하면 무한 로딩이 됩니다.
서버 요청 방식이 delete일 때는 응답.send가 안 되는 것인가요? 제 생각이 틀렸다면 혹시 이유가 무엇인가요?