프론트엔드 웹/Next

Next.js getInitialProps / getServerSideProps 예제

세리둥절 2022. 3. 21. 20:16
반응형
Sample.getInitialProps = async () => {
  const { data } = await axios.get(
    encodeURI(
      `http://alb-diaas-07480.ap-northeast-2.elb.amazonaws.com:3000/왕십리`
    )
  );

  return data;
};
export async function getServerSideProps() {
  const data = await axios
    .get(
      encodeURI(
        `http://alb-diaas-07480.ap-northeast-2.elb.amazonaws.com:3000/왕십리`
      )
    )
    .then((res) => {
      console.log("response");
      return res.data.data[0];
    })
    .catch((error) => {
      return error.message;
    });

  return {
    props: {
      data,
    },
  };
}
반응형