-
글쓴이글
-
2022년 9월 24일 19:53 #47670
코딩이참가자const express = require("express"); const app = express(); const bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: true })); const MongoClient = require("mongodb").MongoClient;
app.set("view engine", "ejs");
var db; MongoClient.connect( "mongodb+srv://gihwan:@atlascluster.u961rpo.mongodb.net/?retryWrites=true&w=majority", function (err, client) { if (err) return console.log(err);
db = client.db("todoapp");
app.listen(8080, function () { console.log("listening on 8080"); }); } );
app.get("/pet", function (req, res) { res.send("반갑습니다"); });
app.get("/beauty", function (req, res) { res.send("고맙습니다"); });
app.get("/", function (req, res) { res.sendFile(__dirname + "/write.html"); }); app.get("/index", function (req, res) { res.sendFile(__dirname + "/index.html"); });
app.post("/add", function (요청, 응답) { 응답.send("전송완료"); db.collection("counter").findOne( { name: "게시물갯수" }, function (에러, 결과) { var 총게시물갯수 = 결과.totalPost; db.collection("post").insertOne( { _id: 총게시물갯수 + 1, 제목: 요청.body.title, 날짜: 요청.body.date }, function () { console.log("저장완료"); db.collection("counter").updateOne( { name: "게시물갯수" }, { $inc: { totalPost: 1 } }, function (에러, 결과) { if (에러) { return console.log(에러); } } ); } ); } ); });
app.get("/list", function (요청, 응답) { db.collection("post") .find() .toArray(function (에러, 결과) { console.log(결과); 응답.render("list.ejs", { posts: 결과 }); }); });
app.delete("/delete", function (요청, 응답) { 요청.body._id = parseInt(요청.body._id); db.collection("post").deleteOne(요청.body, function (에러, 결과) { console.log("삭제완료"); }); 응답.send("삭제완료"); });
server.js 파일이고요
<!DOCTYPE html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<!-- Bootstrap CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous" />
<title>Hello, world!</title> </head> <body> <h1 class="ml-2 my-3">오늘 할 일</h1>
<% for (var i=0; i< posts.length ; i++){ %> <ul class="list-group"> <li class="list-group-item"> <h4>할일 제목 : <%= posts[i].제목 %></h4> <h5>할일 제목 : <%= posts[i].날짜 %></h5> <button>삭제</button>
<% } %>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script> $.ajax({ method: "DELETE", url: "/delete", data: { _id: 1 }, }).done(function (결과) { console.log("delete success"); }); </script>
<!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous" ></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous" ></script> </body> </html>
list.ejs 파일입니다. 삭제가 안되고 cannot get /delete 라고 화면이 뜹니다 해결 방법을 좀 알려주세요
2022년 9월 24일 21:30 #47676
codingapple키 마스터/delete 안만들어놨다는 에러같은데 파일저장했나 확인합시다 글에 db계정 비번도 함께 올리시면 안됩니다
2022년 9월 25일 10:27 #47733
codingapple키 마스터list.ejs 방문할 때 마다 /delete로 delete 요청보내라고 코드짠거같은데 크롬콘솔창 등에 에러같은건 안뜨나요
-
글쓴이글
- 답변은 로그인 후 가능합니다.