2 글 보임 - 1 에서 2 까지 (총 2 중에서)
-
글쓴이글
-
2023년 8월 18일 02:11 #95010
김동현참가자nodejs에서 kogpt를 불러와서 결과값을 react 보여주고 싶습니다.
질문1.영상에서는 route dom을 사용해서 app.use(express.static(path.join(__dirname,'client/build'))); 이러한 미들웨어 때문에
서버에서 get 요청을 작성해도 페이지상단에서 url 작성한걸로는 get 요청이 이루어지지 않는데 다른 방법이 있을까요?
질문2. 위의 문제 때문에 react에서 아래와 같이 axios 요청을 해서 데이터를 사용할려고 시도해 보았는데 data에는 <!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/>
이런 이상한 html 태그들만 잔뜩 저장이 되어있는데 어떻게 node에서 작성한 코드를 react에서 가져올 수 있을까요?
useEffect(()=>{ axios.get('/server/kogpt') .then(result=>{ console.log('axios 결과값:'+result.data); setData(result.data); }).catch(e=>{ console.log('axios 에러낫어욤!!:'+e); }) },[])
질문3.nodejs 를 개발하다가 react에 변경점이 생겨서 수정하고 테스트를 할려고 하면 npm run start의 경우는 server의 데이터를 가져올수가 없어서
npm run build 를 실행해줘야 하는데 수정할때마다 build 를 해줘야만 하나요?
질문4.node 파일안에 react 파일이 저장되어있는 형식인데 배포할때는 다시 파일을 분리해서 서버와 react를 따로 배포를 해야 하나요?
현재 nodejs 코드는 아래와 같습니다
app.listen(8080,function(){ console.log('listening on 8080'); }); app.use(express.static(path.join(__dirname,'client/build')));
app.get('/',function(req,res){ res.sendFile(path.join(__dirname,'client/build/index.html')); })
app.get('*',function(req,res){ res.sendFile(path.join(__dirname,'client/build/index.html')); })
app.get('/server/kogpt', function (req, res) { console.log('running api'); try { axios.post(url, data, { headers }) .then(response => { console.log('요청 성공!'); console.log('Response:', response.data); res.send(response.data); }) .catch(error => { console.error(`서버 axios 에러: ${error}`); res.status(500).send('서버 axios 에러'); }); } catch (error) { console.error(`서버 try문 에러: ${error}`); res.status(500).send('서버 try문 에러'); } });
2023년 8월 18일 09:53 #95031
codingapple키 마스터서버로 ajax요청해서 데이터 가져옵시다 서버는 html 말고 응답.send나 .json으로 데이터만 보내주면 됩니다 서버나 리액트나 둘 다 미리보기 띄워놓으면 변경사항 반영됩니다 서버프로젝트 안에 리액트있으면 그냥 리액트폴더에서 npm run build한 다음 서버프로젝트 올리면 끝입니다
-
글쓴이글
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
- 답변은 로그인 후 가능합니다.