Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- ejs 문법
- updatemany
- typescript #react #jsx #component #usestate
- expess
- typescript #import #export #*
- Passport
- GitHub Actions
- Promise #async #await #try #catch
- typescript #constructor #class #public #private
- github#githubpages#gh-pages#deploy
- deserializeUser
- insertone
- git vscode
- serializeUser
- git#github#init#add#commit#restore#log#staging area#repository#git사용법
- TypeScript #문법
- __dirname
- mongodb 입출력
- CSS
- updateone
- typescript #class #constructor #public #private #static #extends #protected #typeof #in #instanceof #never
- 정적 페이지
- typescript #unknown
- typescript #class #extends #public #private #protected #static
- connect-mongo
- react#로스트아크#mbti#테스트
- github#githubpages#빈화면#basename
- github#로컬저장소#원격저장소
- e.target
- sendFile
Archives
- Today
- Total
VaIice의 이야기
[Node.js] 서버로 데이터 보내기, 게시글 삭제 만들기 본문
1. 주소창, <a>, <form>
사용하면 새로고침
2. ajax - fetch
새로고침 없이 API 요청 가능
body 안에 전송
3. URL 파라미터
fetch(`/delete/<%-post._id%>`, {
4. query string
fetch(`/delete?id=<%-post._id%>`, {
method: 'DELETE',
headers : {
'Content-Type' : 'application/json'
}
?변수=값&변수=값
서버에서 request.query로 사용
5. 삭제 코드
// server.js
app.delete('/delete/:id', async (request, response) => {
try {
await db.collection('main').deleteOne({ _id: new ObjectId(request.params.id) });
response.sendStatus(204);
}
catch {
response.redirect('/')
}
})
POST는 redirect가 가능하지만, DELETE는 안 된다고 한다.
// detail.ejs
function deletePost() {
function deletePost() {
fetch(`/delete/<%-post._id%>`, {
method: 'DELETE',
headers : {
'Content-Type' : 'application/json'
}
})
.then(response => {
if (response.ok) {
window.location.href = '/'
}
})
}
때문에 JS에서 직접 주소를 옮겨주는 코드를 작성해야 한다.
'[Node.js]' 카테고리의 다른 글
[Node.js] Session, Token, OAuth, passport (2) | 2024.07.16 |
---|---|
[Node.js] 페이지 기능 만들기 (0) | 2024.07.16 |
[Node.js] 게시글 수정 만들기 (0) | 2024.07.15 |
[Node.js] 상세 페이지 만들기 (0) | 2024.07.14 |
[Node.js] 글쓰기 만들기 (0) | 2024.07.11 |