반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

taenyLog

Express Post 요청 접근 본문

Web

Express Post 요청 접근

taenyLog 2023. 6. 28. 10:01
반응형

post 요청을 받았을때 req.body 접근위해 아래와 같은 코드를 index.js파일에 넣어준다.

// 우리가 post 요청을 받으면 그 body의 정보가 필요로 하지만 req.body로 바로 접근 불가
// 할 순 있지만 Undefined 값을 갖는다 .파싱되지않는값
// Express가 미들웨어를 사용하도록 명령해야함.
app.use(express.urlencoded({ extended: true }));

 

 

html

 <form action="/products" method="POST">
        <label for="name">Product Name</label>
        <input type="text" name="name" id="name" placeholder="product name">
        <label for="price">Price (Unit)</label>
        <input type="number" id="price" name="price" placeholder="price">
        <label for="category">Select Category</label>
        <select name="category" id="category">
            <% for(let category of categories){ %>
            <option value="<%=category%>"><%=category%></option>
            <% } %>
        </select>
        <button>Submit</button>
    </form>
반응형

'Web' 카테고리의 다른 글

Mongoose | findByIdAndDelete()  (0) 2023.06.29
Mongoose | findByIdAndUpdate()  (0) 2023.06.29
http://localhost:3000 에러  (0) 2023.06.28
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 에러 해결  (0) 2023.06.28
res.send res.render  (0) 2023.06.28