• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Node.js, Express 게시판 submit 버튼 클릭시 페이지가 뜨지않는 문제

submit 버튼 클릭시 페이지가 뜨지않는 문제

  • 이 주제에는 5개 답변, 2명 참여가 있으며 chaey2 년, 4 월 전에 전에 마지막으로 업데이트했습니다.
6 글 보임 - 1 에서 6 까지 (총 6 중에서)
  • 글쓴이
  • #73151

    chaey
    참가자
    405 에러는 post->get으로 변경하여 더이상 뜨지않는데, submit 클릭시 아예 데이터가 없다고 뜨는것같습니다.
    여러번 검토해봤는데 , 뭐가 문제인지 알수가없습니다.
    코드 남깁니다. 또, 혹시 전체 코드가 올라온 자료같은건 없을까요?
    
    index.html
    
    <!DOCTYPE html>
    <html>
      <head>
        <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.6.2/dist/css/bootstrap.min.css"
          integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N"
          crossorigin="anonymous"
        />
        <title>Hello, world!</title>
      </head>
      <body>
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <button
            class="navbar-toggler"
            type="button"
            data-toggle="collapse"
            data-target="#navbarTogglerDemo03"
            aria-controls="navbarTogglerDemo03"
            aria-expanded="false"
            aria-label="Toggle navigation"
          >
            <span class="navbar-toggler-icon"></span>
          </button>
          Todo App
          <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
            <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
              <li class="nav-item active">
                <a class="nav-link" href="#"
                  >Home <span class="sr-only">(current)</span></a
                >
             

              <li class="nav-item">             Write          

              <li class="nav-item">             임시          

           

        <!-- Optional JavaScript; choose one of the two! -->
        <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
        <script
          src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"
          integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
          crossorigin="anonymous"
        ></script>
        <script
          src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"
          integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct"
          crossorigin="anonymous"
        ></script>
      </body>
    </html>
    
    
    write.html
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <link
          rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
        />
        <title>Document</title>
      </head>
      <body>
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <button
            class="navbar-toggler"
            type="button"
            data-toggle="collapse"
            data-target="#navbarTogglerDemo03"
            aria-controls="navbarTogglerDemo03"
            aria-expanded="false"
            aria-label="Toggle navigation"
          >
            <span class="navbar-toggler-icon"></span>
          </button>
          Todo App
          <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
            <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
              <li class="nav-item active">
                <a class="nav-link" href="#"
                  >Home <span class="sr-only">(current)</span></a
                >
             

              <li class="nav-item">             Write          

              <li class="nav-item">             임시          

        <script
          src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
          integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
          crossorigin="anonymous"
        ></script>
        <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://stackpath.bootstrapcdn.com/bootstrap/4.4.1/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;
    var db;
    MongoClient.connect(
      "mongodb+srv://admin:@cluster0.zb68zx8.mongodb.net/?retryWrites=true&w=majority",
      function (에러, client) {
        //연결되면 할일
        if (에러) return console.log(에러);
        db = client.db("todoapp");
        db.collection("post").insertOne(
          { 이름: "chaey", _id: 100, 나이: 27 },
          function (에러, 결과) {
            console.log("저장완료");
          }
        );
        app.listen(8080, function () {
          console.log("listening on 8080");
        });
      }
    );
    
    app.get("/", function (요청, 응답) {
      응답.sendFile(__dirname + "/index.html");
    });
    // /하나만 쓰면 홈페이지 , .sendFile(보낼경로)
    app.get("/write", function (요청, 응답) {
      응답.sendFile(__dirname + "/write.html");
    });
    // 어떤사람이 /add 경로로 POST 요청을 하면..
    // ~를 해주세요
    app.get("/newPost", function (요청, 응답) {
      응답.send("전송완료");
      console.log(요청.body.date);
      console.log(요청.body.title);
    });
    
    진도를 나가지못하고 있습니다 ㅠㅠ
    #73165

    codingapple
    키 마스터
    폼으로 데이터전송하려면 get말고 post로 바꿉시다
    #73167

    chaey
    참가자
    post 로 작성해도 오류가 뜹니다!
    #73174

    chaey
    참가자
    post로 작성시 submit 버튼 눌렀을때 아래처럼 뜹니다
    
    지금 이 페이지가 작동하지 않습니다.
    문제가 계속되면 사이트 소유자에게 문의하세요.
    HTTP ERROR 405
    #73179

    codingapple
    키 마스터
    form태그 method도 post로 
    서버코드도 app.post로 바꿉시다
    #73334

    chaey
    참가자
    다 바꿨습니다! 그렇지만 연결되지않습니다..
6 글 보임 - 1 에서 6 까지 (총 6 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠