2 글 보임 - 1 에서 2 까지 (총 2 중에서)
-
글쓴이글
-
2022년 9월 27일 15:10 #48026
코딩이참가자제대로 test는 마이페이지입니다 라고 뜨는데 계속 에러가 뜹니다 이유를알려주세요 mypage.ejs 파일이고요
<!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" />
<link rel="stylesheet" href="/public/main.css" />
<title>Hello, world!</title> </head> <body> <%- include('nav.html') %>
<h1><%= 사용자.id %>의 마이페이지 입니다.</h1> <!-- 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>
이거는 server.js 파일입니다
const express = require("express"); const app = express(); const bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: true })); const MongoClient = require("mongodb").MongoClient; const methodOverride = require("method-override"); app.use(methodOverride("_method")); app.set("view engine", "ejs");
app.use("/public", express.static("public"));
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.render("write.ejs"); }); app.get("/index", function (req, res) { res.render("index.ejs"); });
app.get("/write", function (req, res) { res.render("write.ejs"); });
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("삭제완수"); }); 응답.status(200).send({ message: "성공했습니다" }); 응답.send("삭제완료"); });
app.get("/detail/:id", function (요청, 응답) { db.collection("post").findOne( { _id: parseInt(요청.params.id) }, function (에러, 결과) { 응답.render("detail.ejs", { data: 결과 }); console.log(결과); } ); });
app.get("/edit/:id", function (요청, 응답) { db.collection("post").findOne( { _id: parseInt(요청.params.id) }, function (에러, 결과) { 응답.render("edit.ejs", { post: 결과 }); } ); });
app.put("/edit", function (요청, 응답) { db.collection("post").updateOne( { _id: parseInt(요청.body.id) }, { $set: { 제목: 요청.body.title, 날짜: 요청.body.date } }, function (에러, 결과) { console.log("수정완료"); 응답.redirect("/list"); } ); });
const passport = require("passport"); const LocalStrategy = require("passport-local").Strategy; const session = require("express-session");
app.use( session({ secret: "비밀코드", resave: true, saveUninitialized: false }) ); app.use(passport.initialize()); app.use(passport.session());
app.get("/login", function (요청, 응답) { 응답.render("login.ejs"); });
app.post( "/login", passport.authenticate("local", { failureRedirect: "/fail", }), function (요청, 응답) { 응답.redirect("/"); } );
app.get("/mypage", 로그인했니, function (요청, 응답) { 응답.render("mypage.ejs"); 응답.render("mypage.ejs", { 사용자: 요청.user }); });
function 로그인했니(요청, 응답, next) { if (요청.user) { next(); } else { 응답.send("로그인안하셨는데요?"); } }
passport.use( new LocalStrategy( { usernameField: "id", passwordField: "pw", session: true, passReqToCallback: false, }, function (입력한아이디, 입력한비번, done) { //console.log(입력한아이디, 입력한비번); db.collection("login").findOne( { id: 입력한아이디 }, function (에러, 결과) { if (에러) return done(에러);
if (!결과) return done(null, false, { message: "존재하지않는 아이디요" }); if (입력한비번 == 결과.pw) { return done(null, 결과); } else { return done(null, false, { message: "비번틀렸어요" }); } } ); } ) );
passport.serializeUser(function (user, done) { done(null, user.id); });
passport.deserializeUser(function (아이디, done) { db.collection("login").findOne({ id: 아이디 }, function (에러, 결과) { done(null, 결과); }); });
-
글쓴이글
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
- 답변은 로그인 후 가능합니다.