반응형
리액트 노드 값 주고받기
리액트
useEffect(() => {
const fetchData = async () => {
const response = await fetch('/list/company', {
method: 'POST',
headers: {
"Content-Type": "application/json",
}, // json형태의 데이터를 서버로 보냅니다.
body: JSON.stringify({
id: title
})
})
const json = await response.json();
setFarm(json);
};
fetchData();
}, []);
노드
app.post('/list/company', (req, res) => {
console.log("리퀘 바디 : "+req.body.id)
// collection = 테이블 이름
db.collection('drList').find({title : req.body.id}).toArray((err, result) => {
console.log(result)
res.json(result);
});
});
주의
req body 가 undefined 일때
const bodyParser = require('body-parser')
app.use(bodyParser.json());
바디파서 추가하셈
반응형
'JavaScript > React' 카테고리의 다른 글
#props #부모 state 갖다 쓰기 (0) | 2023.02.14 |
---|---|
리액트 반복 출력 # map (0) | 2023.02.14 |
#동적인 UI 만들기 #state 이용 # onclick Modal (0) | 2023.02.13 |
#component #리액트 컴포넌트 만드는법 # 컴포넌트 사용법 (0) | 2023.02.13 |
#JSX 기본문법 #react style #state (0) | 2023.02.13 |