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>
반응형