반응형

 

 

react 프로젝트에서 svg를 import 시키고 컴포넌트를 아래와 같이 추가하였다

import {useRouter} from "next/router";

import GoogleSignIn from '@/assets/googleIcon.svg'

export const JoinGoogle = () => {

    function socialLoginGoogle() {
        ///////
    }

    return (
        <>
            <button onClick={socialLoginGoogle}>
                <GoogleSignIn />
                구글로 가입하기
            </button>
        </>
    )
}


별다른 문제가 없을거라 생각했는데, 오류가 발생하였다

 

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

 

터미널 로그에서는 <GoogleSignIn/>을 확인하라고 보여진다

 

Check your code at JoinGoogle.tsx:21.

 

확인해보니, svg파일을 사용하려면 별도의 플러그인을 install 해야한다고 함...

 

해결방법

 

1. install svgr webpack 

npm install -D @svgr/webpack

 

터미널에서 svgr webpack 을 다운받는다

 

2. next.config.js 수정

/** @type {import('next').NextConfig} */
const nextConfig = {

....
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/,
      use: ['@svgr/webpack'],
    })
    return config
  },
  
  .....
}

module.exports = nextConfig

 

webpack 설정을 추가해준다

728x90
반응형

localhost:3000 이라는 포트로 만들어진 react 프로젝트에서

다른 포트번호로 실행된 백단 프로젝트를 연결할때 api cors error 를 필연적으로 만나게된다.

 

해결방법은 간단하다

 

1. next.config.js 수정

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: false, //true 면 next dev 실행할때 useEffect 2번씩 실행
  swcMinify: true,
  eslint: {
    ignoreDuringBuilds: true,
  },
  compiler: {
    styledComponents: true,
  },
  images: {
    unoptimized: true,
    domains: ['picsum.photos', 'img.dmitory.com', 'image.edaily.co.kr'],
  },
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/,
      use: ['@svgr/webpack'],
    })
    return config
  },
  //api cors 에러 프록시 설정
  async rewrites() {
    return [
      {
        source: '/api-aaa/:path*',
        destination: 'http://localhost:8080/api-aaa/:path*',
      },
      {
        source: '/api-bbb/:path*',
        destination: 'http://localhost:8090/api-bbb/:path*',
      },
      {
        source: '/api-ccc/:path*',
        destination: 'http://localhost:8443/api-ccc/:path*',
      },
      {
        source: '/upload/:path*',
        destination: 'http://localhost:8443/upload/:path*',
      },
    ]
  },
}

module.exports = nextConfig

 

 

rewrites 안쪽에 내가 통신하려고 하는 서버의 포트번호를 넣어주면 된다

나의 경우 멀티모듈을 사용했기때문에 저렇게 많은 포트가 필요했다

 

source 설정

지난 포스팅에 있는것처럼 api 호출할때 아래와 같은식으로 호출되는 부분이 있다고 한다면, 

getMenu = async ()=> {
    return await client.get('/api-aaa/menu/list')
}

 

/api-aaa/ 를 prefix로 삼고 api-aaa 로 통신하는 모든 uri들은 포트 8080으로 가게끔 경로를 틀어준다는 의미이다

 

destination 설정

만약 로컬에서 프론트/백단 소스를 모두 띄운다면 위 코드는 문제가 없겠지만

 

로컬에서 프론트 실행하고, 다른 서버에서 백단을  실행하거나

프론트/백단 소스코드를 모두 다른 서버(aws, linux 등..) 에서 실행한다면 destination 을 변경해야한다

...

async rewrites() {
    return [
      {
        source: '/api-aaa/:path*',
        destination: 'http://서버IP:8080/api-aaa/:path*',
      },
      {
        source: '/api-bbb/:path*',
        destination: 'http://서버IP:8090/api-bbb/:path*',
      },
      {
        source: '/api-ccc/:path*',
        destination: 'http://서버IP:8443/api-ccc/:path*',
      },
      {
        source: '/upload/:path*',
        destination: 'http://서버IP:8443/upload/:path*',
      },
    ]
  },

...

 

localhost 부분을 서버IP로 바꿔주면 된다.

 

 

2. 백단 소스코드수정

만약 WebConfig 를 설정하는 소스코드가 있다면 해당부분을 수정하고 없다면 새로만들어주자

@Configuration 으로 전체검색했을때 없으면 없는것

 

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${config.server-url}")
    String serverUrl;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://" + serverUrl + ":3000") // 프론트의 주소와 포트번호
                .allowedMethods("GET", "POST")
                .allowCredentials(true);
    }

}

 

 

나는 get 과 post만 열어줬고 이 설정만 추가하면 잘될것!!

 

혹시 allowedOrigins에 다른 ip주소도 추가해야한다면 아래와같이 콤마로 추가할수있다

.allowedOrigins("http://" + serverUrl + ":3000", "http://어쩌고ip:3000")

 

 

그래도 안된다면, spring security 사용하고있는경우 security 설정에서 막고있는게 아닌지 확인해야 한다.

 

그래도 안된다면!! IP주소 혹은 API PATH 오타가 있지는 않은지 확인해보자

728x90
반응형

 

 

 

 [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Cannot find module 'mini-css-extract-plugin/dist/CssDependency'

 

.next 의 cache 폴더 삭제

728x90
반응형

 

 

나의 경우는 백단 서버가 이미 존재하여, URI로 요청하는 방식이므로 axios를 사용했다

axios 사용법에 앞서, 기본적인 react 문법을 훑고 지나가자

 

기본 react 문법

아래는 임시로 작성한 Mypage 이다

import {NextPageWithLayout} from "@/pages/_app";
import Layout from "@/components/layout/Layout";
import {useState} from "react";

interface itemListProps {
    id: string
    title: string
}

const MyPage: NextPageWithLayout = () => {

    //기본적인 변수 선언법
    const [nickname, setNickname] = useState<string>('')
    const [age, setAge] = useState<number>(0)
    const [isModalOpen, setIsModalOpen] = useState(false)
    const [itemList, setItemList] = useState<itemListProps[]>([])

    return(
        <div> mypage </div>
        <div>
        	{itemList.map((item, index) => (
                <div key={item.id}>{item.title}</div>
            ))}
    )
}

MyPage.getLayout = function getLayout(page) {
    return <Layout>{page}</Layout>
}

export default MyPage

 

useState

변수선언은 useState 를 이용하여 선언한다. 우측 괄호 안에 값을 넣는 것으로 초기화 가능하다

또한 List 선언의 경우 type을 정해서 itemListProps 라는 type을 정해서 리스트화 시켜줬다.

 

parameter type

특히 api와 통신하는 값을 받는 리스트의 경우 위와 같이 type을 interface로 선언해주고 사용하자.

n명의 개발자가 동시에 개발할때는 type이 정해져있지않다면 중구난방 난리가 날것이기 때문...

 

자세한 상황은 아래 api 연결할때 설명하겠다

Map

리스트의 경우 map 을 이용하여 풀어낼 수 있다.

첫번째 인자로 객체가 들어가고, 두번째는 index가 오게되는데 map 안쪽에 첫번째 요소에 key 속성이 꼭 있어야한다

만약 아래와 같이 코드를 만들경우 에러가 날것이다

return(
    <div> mypage </div>
    <div>
        {itemList.map((item, index) => (
            <div>{item.title}</div>
        ))}
)

 

key를 꼭 붙여주도록 하자

 

 

install axios

프로젝트의 루트위치에서 터미널을 실행하고 아래와 같은 커맨드를 입력한다

$ npm install axios

 

pakage.json, package-lock.json 파일이 자동으로 수정될것이다

 

axios 설정

루트폴더에 api 폴더를 생성하고 client.tsx 파일을 생성하자

 

이곳에 axios 의 모든 설정을 세팅할것이다

get, post, multipartfile을 같이 보낼경우 3가지의 경우를 연결하겠다

 

▼client.tsx

import axios from 'axios';


class Client {

    async get(url: string, params?: any) {
        try {
            const headers = {
                'Content-Type': 'application/json',
                'X-Requested-With': 'XMLHttpRequest',
            }

            const response = await axios.get(url, {
                params: params,
                headers: headers,
            });
            return response.data;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    async post(url: string, params?: any) {
        try {
            const headers = {
                'Content-Type': 'application/json',
                'X-Requested-With': 'XMLHttpRequest',
            }

            const response = await axios.post(url, params, {
                headers: headers,
            });
            return response.data;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    async postMultipartFile(url: string, params: FormData) {
        try {
            const headers = {
                'Content-Type': 'multipart/form-data',
                'X-Requested-With': 'XMLHttpRequest',
            }
            const response = await axios.post(url, params, {
                headers: headers,
            });
            return response.data;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

}

export const client = new Client();

 

이렇게 소스를 따로 뗀 이유는 api 통신이 필요한 프로젝트 내 모든 페이지에서 별도로 연결을 해주어야하기 때문이다

가독성에 안좋고 스프링시큐리티 등의 이유로 인하여 header에 특정 텍스트/쿠키 등을 태워서 보내야 하는 경우 이곳에서 한번에 처리가 가능하다.

 

axios 적용 테스트

자 이제 실제 페이지에서 사용해봅시다

const MyPage: NextPageWithLayout = () => {

    //기본적인 변수 선언법
    const [nickname, setNickname] = useState<string>('')
    const [age, setAge] = useState<number>(0)
    const [isModalOpen, setIsModalOpen] = useState(false)
    const [itemList, setItemList] = useState<itemListProps[]>([])

    useEffect(() => {
        client.get("/api/어쩌고저쩌고")
            .then((response) => {
                console.log(response)
            }).catch((error) => {
                console.log(error)
            })
    },[])
    
    return(
        <div> mypage </div>
    )
}

 

mypage에 진입하자마자 useEffect 가 실행되고 만들어놓은 axios get 통신을 진행하게된다

그런데 보면... 코드상에서 직접 api path 를 입력하게되는데 딱봐도..너무불안하다

실수로 오타라도 나면 url 호출이 안되면서 에러가 빡빡 생길 조짐이 보인다...

그리고 path 가 일괄변경되면 이렇게 호출한 api 로직부분을 하나하나 찾아서 고쳐줘야한다..

 

그래서 api 통신부분을 따로 빼기로했다

axios 실제 적용

 

 

api 폴더 안에 패키지 하나 + tsx 파일 하나 선언해주자

보통 내가 쓰는 도메인 위주로 하면된다

user 관련 api 면 user.. main 페이지 관련 api 면 main.. menu.. category..등등

import {client} from "@/api/client";

class MyPageApi {
    /**
     * ㅇㅇㅇ페이지 :: ㅁㅁㅁ 조회
     */
    test1 = async () => {
        return await client.get('/api/어쩌고주소')
    }

    /**
     * ㅇㅇㅇ페이지 :: ㅁㅁㅁ 조회
     * @param title
     */
    test2 = async (title:string) => {
        return await client.get('/api/어쩌고주소',title)
    }

    /**
     * ㅇㅇㅇ페이지 :: ㅁㅁㅁ 수정
     */
    test3 = async () => {
        return await client.post('/api/어쩌고주소')
    }

    /**
     * ㅇㅇㅇ페이지 :: ㅁㅁㅁ 삭제
     * @param title
     */
    test4 = async (title:string) => {
        return await client.post('/api/어쩌고주소', title)
    }
}

export const myPageApi = new MyPageApi();

 

이렇게 모아두면 설령 api path가 변경되는 일이 있더라도, 한번에 처리하기 쉽다!

실제 페이지 코드에서는 높은 가독성까지!

 

이걸 실제 코드에 적용시키면

import {NextPageWithLayout} from "@/pages/_app";
import Layout from "@/components/layout/Layout";
import {useEffect, useState} from "react";
import {myPageApi} from "@/api/myPage/myPageApi";

interface itemListProps {
    id: string
    title: string
}

const MyPage: NextPageWithLayout = () => {

    useEffect(() => {
      myPageApi.test4('test')
          .then((response:itemListProps) => {
              console.log(response)
          })
          .catch((error) => {
              console.log(error)
          })
    },[])

    return(
        <div> mypage </div>
    )
}

MyPage.getLayout = function getLayout(page) {
    return <Layout>{page}</Layout>
}

export default MyPage

 

파라미터만 url 별도입력없이 파라미터만 던지는 가독성이 깔끔한 로직이 완성된다

728x90

+ Recent posts