λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

Nuxt.js + Spring Boot

πŸ“’ λ“€μ–΄κ°€κΈ° 전에

  • Intellij IDEA Community Edition 2019.3.2 μ—μ„œ μ§„ν–‰ν–ˆλ‹€. 버전이 달라도 상관 μ—†μŒ. 
  • Spring Boot Maven ν”„λ‘œμ νŠΈμ™€ Nuxt.js λ₯Ό μ—°λ™ν•˜λŠ” 방법을 μ•Œμ•„λ³Έλ‹€. Vue.js도 거의 λ™μΌν•œ λ°©λ²•μœΌλ‘œ 진행할 수 μžˆλ‹€.

πŸƒ Spring Boot (Maven)

File - New - Project

File - New - Project 둜 μƒˆ ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν•œλ‹€.

 

Create New Project

초기 ν™”λ©΄μ˜ Create New Project 둜 생성할 μˆ˜λ„ μžˆλ‹€.

 

Maven 선택

ν΄λ¦­ν•˜κ³ λ‚˜λ©΄ μœ„μ™€ 같이 New Project λΌλŠ” 창이 λœ°ν…λ°, μ—¬κΈ°μ„œ 쒌츑의 Maven 을 ν΄λ¦­ν•œλ‹€.

(Gradle ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν•˜κ³ μž 할땐, Gradle 을 선택해주면 λœλ‹€.)

μœ„μͺ½μ˜ Project SDK 도 선택해쀀 λ’€ λ‹€μŒμœΌλ‘œ λ„˜μ–΄κ°„λ‹€. (μ•„λ§ˆ 각자의 ν™˜κ²½μ— μ„€μΉ˜λœ JDK κ°€ 이미 μ„ νƒλ˜μ–΄ μžˆμ„ 것이닀.)

 

Project Name, Location μ„€μ •

 

ν”„λ‘œμ νŠΈμ˜ 이름과 경둜λ₯Ό μ„€μ •ν•΄μ€€λ‹€.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>role-playing-community</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>

pom.xml

 

이제 λλ‚œ κ²ƒμ΄λ‚˜ 닀름 μ—†λ‹€!

Maven ν”„λ‘œμ νŠΈκ°€ 생성될 것이고, pom.xml μ΄λΌλŠ” Maven μ„€μ •νŒŒμΌμ΄ μ‘΄μž¬ν•˜λŠ” κ±Έ 확인할 수 μžˆλ‹€.

πŸš€ κ°„λ‹¨ν•œ pom.xml μ„€λͺ…

  • ν”„λ‘œμ νŠΈμ˜ ꡬ쑰와 λ‚΄μš©μ„ μ„€λͺ…ν•˜κ³  μžˆλŠ” Maven μ„€μ • 파일.
  • ν”„λ‘œμ νŠΈ 관리 및 λΉŒλ“œμ— ν•„μš”ν•œ ν™˜κ²½ μ„€μ •, μ˜μ‘΄μ„± 관리 등을 ν•  수 μžˆλ‹€.
  • <modelVersion>
    • Maven의 pom.xml의 λͺ¨λΈ 버전
  • <groupId>
    • ν”„λ‘œμ νŠΈλ₯Ό λ§Œλ“  그룹의 Id이닀. κΈ°μ—… 이름, λ˜λŠ” νŒ€ 이름이 해당될 수 μžˆλ‹€.
    • κ³ μœ ν•˜κ²Œ 식별할 수 μžˆμ–΄μ•Όν•˜κΈ° λ•Œλ¬Έμ— νŒ¨ν‚€μ§€ λͺ…λͺ… κ·œμΉ™μ„ λ”°λ₯Έλ‹€. λ•Œλ¬Έμ— .으둜 μ›ν•˜λŠ” 만큼 ν•˜μœ„ 그룹을 λ§Œλ“€ 수 μžˆλ‹€.
    • νŒ¨ν‚€μ§€ λͺ…λͺ… κ·œμΉ™μ΄λž€ κ°„λ‹¨νžˆ μ„€λͺ…ν•˜λ©΄ 도메인을 μ—­μˆœμœΌλ‘œ λ‚˜μ—΄ν•œ 것과 κ°™λ‹€.
    • 예) org.apache.maven
  • <artifactId>
    • ν”„λ‘œμ νŠΈμ˜ Id.
    • μ΄ˆκΈ°μ— ν”„λ‘œμ νŠΈλ₯Ό 생성할 λ•Œ Name μž…λ ₯칸에 μž‘μ„±ν–ˆλ˜ ν”„λ‘œμ νŠΈμ˜ 이름.
    • Maven이 μƒμ„±ν•΄λ‚΄λŠ” artifactλŠ” <artifactId>-<version>.<extension> 와 같은 ꡬ쑰λ₯Ό 보인닀.
    • 예) demo-0.0.1-SNAPSHOT.jar
  • <version>
    • ν”„λ‘œμ νŠΈ 버전을 의미.
    • SNAPSHOT은 아직 개발 λ‹¨κ³„λΌλŠ” λœ»μ΄λ‹€.

 

이제 Spring Bootλ₯Ό μ£Όμž…ν•΄λ³΄μž

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>

pom.xml

 

 

μœ„ μ½”λ“œλ₯Ό <project> νƒœκ·Έ 내에 μ‚½μž…ν•œλ‹€.

 

spring-boot-starter-parent 라이브러리λ₯Ό μ•Œμ•„λ³΄κΈ°μ— μ•žμ„œ, <parent> νƒœκ·Έκ°€ 무엇인지 μ•Œμ•„λ³΄μž.

 

Maven 은 상속이 κ°€λŠ₯ν•˜λ‹€λŠ” νŠΉμ§•μ„ 가지고 μžˆλ‹€.
<parent> νƒœκ·Έ 내에 μ„ μ–Έν•œ 라이브러리λ₯Ό λ‹€λ₯Έ μ˜μ‘΄μ„±/ν•˜μœ„ λͺ¨λ“ˆ(<dependency>) 듀이 상속받아 μ“Έ 수 μžˆλ‹€λŠ” λœ»μ΄λ‹€. λ”°λΌμ„œ <parent>λŠ” κ³΅ν†΅μœΌλ‘œ μ‚¬μš©ν•˜λŠ” μ„€μ • 등을 μ£Όμž…ν•  λ•Œ 쓰인닀.

 

spring-boot-starter

spring-boot-starter-parent에 λŒ€ν•΄ μ„€λͺ…ν•˜κΈ° 전에 spring-boot-starter 에 λŒ€ν•΄ μ•Œμ•„λ³΄κ² λ‹€.
spring-boot-starterλŠ” 사전에 미리 μ •μ˜ν•œ νŽΈλ¦¬ν•œ μ˜μ‘΄μ„± 쑰합을 μ œκ³΅ν•˜λŠ” Spring Boot λͺ¨λ“ˆμ΄λ‹€.
ν”„λ‘œμ νŠΈμ—μ„œ μ„€μ •ν•΄μ•Όν•˜λŠ” λ‹€μˆ˜μ˜ μ˜μ‘΄μ„±μ„ ν¬ν•¨ν•˜κ³  있기 λ•Œλ¬Έμ—, spring-boot-starterλ₯Ό μ •μ˜ν•΄ μ£ΌλŠ” 것 λ§ŒμœΌλ‘œλ„ λ‹€μ–‘ν•œ μ˜μ‘΄μ„±μ„ μΆ”κ°€ν•œ 효과λ₯Ό λ³Ό 수 μžˆλ‹€.
μ‰½κ²Œ ν•œλ§ˆλ””λ‘œ μ •μ˜ν•˜μžλ©΄, Spring Bootλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•œ 톡합 λΌμ΄λΈŒλŸ¬λ¦¬μ΄λ‹€.
spring-boot-starterκ°€ 가지고 μžˆλŠ” λ‹€μ–‘ν•œ μ˜μ‘΄μ„±μ€ μ—¬κΈ°μ—μ„œ 확인해 λ³Ό 수 μžˆλ‹€.

 

spring-boot-starter-parent

κ·Έλ ‡λ‹€λ©΄ μš°λ¦¬κ°€ μ£Όμž…ν•œ spring-boot-starter-parentλŠ” λ¬΄μ—‡μΌκΉŒ?
ν”„λ‘œμ νŠΈμ— λ‹€μ–‘ν•œ λΌμ΄λΈŒλŸ¬λ¦¬λ“€μ„ μ£Όμž…ν•˜κ²Œλ˜λ©΄ 라이브러리 버전 κ°„ μΆ©λŒλ¬Έμ œκ°€ λ°œμƒν•  수 μžˆλ‹€.
spring-boot-starterκ°€ μ˜μ‘΄μ„± 쑰합을 μ œκ³΅ν•΄ μ€€λ‹€λ©΄, spring-boot-starter-parentλŠ” ν•΄λ‹Ή μ˜μ‘΄μ„± μ‘°ν•©κ°„μ˜ μΆ©λŒλ¬Έμ œκ°€ μ—†λŠ” κ²€μ¦λœ 버전 정보 쑰합을 μ œκ³΅ν•œλ‹€.
spring-boot-starter-parentλ₯Ό μ •μ˜ν•΄μ£Όλ©΄ <dependencies> 에 μž…λ ₯ν•  λΌμ΄λΈŒλŸ¬λ¦¬λ“€μ˜ 버전을 적어주지 μ•Šμ•„λ„ μžλ™ μ„€μ •λœλ‹€.

 

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

pom.xml

 

μ§€κΈˆκΉŒμ§€ 우린 (버전관리가 κ°€λŠ₯ν•œ) Spring Boot 톡합 라이브러리λ₯Ό 섀정해쀬닀.
이제 μœ„ μ½”λ“œλ₯Ό <project> νƒœκ·Έ 내에 μ‚½μž…ν•˜μ—¬ 본격적인 Spring Boot μ›Ή κ°œλ°œμ„ μœ„ν•œ 라이브러리λ₯Ό μ£Όμž…ν•΄μ£Όμž.

 

spring-boot-starter-web

Springμ—μ„œ μ œκ³΅ν•˜λŠ” μ˜μ‘΄μ„±μœΌλ‘œ, Web κ°œλ°œμ— ν•„μš”ν•œ 섀정을 μ œκ³΅ν•œλ‹€.
Sppring MVC, REST, Tomcat μ„œλ²„λ₯Ό λ‚΄μž₯ν•˜κ³  μžˆλ‹€.

 

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

pom.xml

 

Maven 은 λΉŒλ“œ νˆ΄μ΄λ‹€. λ•Œλ¬Έμ— pom.xmlμ—μ„œ λΉŒλ“œκ΄€λ ¨ 섀정을 ν•  수 μžˆλ‹€.
<project> νƒœκ·Έ ν•˜μœ„μ— μœ„ μ½”λ“œλ₯Ό μ‚½μž…ν•˜μ—¬ λΉŒλ“œ μ‹œ ν•„μš”ν•œ ν”ŒλŸ¬κ·ΈμΈμ„ μ£Όμž…ν•΄μ£Όμž.
spring-boot-maven-plugin 은 μ‹€ν–‰κ°€λŠ₯ν•œ jar νŒŒμΌμ„ λ§Œλ“€μ–΄μ£ΌλŠ” ν”ŒλŸ¬κ·ΈμΈμ΄λ‹€.

 

 

Spring Boot Maven ν”„λ‘œμ νŠΈλ₯Ό μ‹€ν–‰ν•˜λŠ”λ° ν•„μš”ν•œ μ΅œμ†Œν•œμ˜ μ˜μ‘΄μ„ λͺ¨λ‘ μ£Όμž…ν–ˆλ‹€!
pom.xml을 건듀면 였λ₯Έμͺ½ ν•˜λ‹¨μ— μœ„μ™€ 같은 νŒμ—…μ΄ λœ¨λŠ”κ±Έ 확인할 수 μžˆμ—ˆλ‹€.
Import Changes λ₯Ό 클릭해 μ£Όμž…ν•œ 라이브러리λ₯Ό Import ν•  수 μžˆλ‹€.

 

pom.xml - Maven - Reimprt

νŒμ—…μ΄ λœ¨μ§€ μ•ŠλŠ”λ‹€λ©΄ pom.xml 였λ₯Έμͺ½ 마우슀 클릭 - Maven - Reimport λ‘œλ„ κ°€λŠ₯ν•˜λ‹€.

 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args); // start whole Spring Framework
    }
}

Application.java

 

이제 Spring Boot 섀정은 끝났닀. μ‹€μ œλ‘œ μ„œλ²„λ₯Ό κ°€λ™μ‹œμΌœλ³΄μž.
src/main/java 폴더에 νŒ¨ν‚€μ§€λ₯Ό λ§Œλ“€κ³  νŒ¨ν‚€μ§€ 내에 Application.java νŒŒμΌμ„ μƒμ„±ν•œ λ’€ μœ„ μ½”λ“œλ₯Ό μž…λ ₯ν•œλ‹€.
Spring Boot λ₯Ό μ‹€ν–‰μ‹œν‚€λŠ” main νŒŒμΌμ΄λ‹€.

 

Run

이 νŒŒμΌμ„ Run μ‹œν‚€λ©΄ Spring Boot μ„œλ²„κ°€ κ°€λ™λ˜λŠ” λͺ¨μŠ΅μ„ λ³Ό 수 μžˆλ‹€!πŸŽ‰

 

πŸš€ [μ°Έκ³ ] Runner λ‘œκ·Έμ°½μ— 색 μž…νžˆλŠ” 법

Spring Boot μ‹€ν–‰ Log μ°½

Spring Boot λ₯Ό μ‹€ν–‰ν•˜κ³  λ‚˜λ©΄ 둜그 창이 μœ„μ™€ 같이 λœ°ν…λ°, 흰 κΈ€μ”¨λ‘œλ§Œ λ˜μ–΄ μžˆμ–΄μ„œ λ‹¨μ‘°λ‘œμš΄ λŠλ‚Œμ΄ λ“ λ‹€.
이 λ‹¨μ‘°λ‘œμš΄ Runner λ‘œκ·Έμ°½μ„ application.properties μ„€μ • νŒŒμΌμ„ 톡해 μ»€μŠ€ν„°λ§ˆμ΄μ§• 해쀄 수 μžˆλ‹€. 

application.properties
μ™ΈλΆ€ μ„€μ • νŒŒμΌμ΄λΌκ³ λ„ ν•˜λ©° μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μ‚¬μš©ν•˜λŠ” μ—¬λŸ¬κ°€μ§€ μ„€μ • 값듀을 μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ λ°–μ΄λ‚˜ μ•ˆμ— μ •μ˜ν•  수 μžˆλŠ” νŒŒμΌμ„ λœ»ν•¨. 
이 νŒŒμΌμ€ μŠ€ν”„λ§ λΆ€νŠΈκ°€ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ„ ꡬ동할 λ•Œ μžλ™μœΌλ‘œ λ‘œλ”©λ˜λŠ” 파일둜, key - value ν˜•μ‹μœΌλ‘œ 값을 μ •μ˜ν•˜λ©΄ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ @Value μ–΄λ…Έν…Œμ΄μ…˜ λ“±μœΌλ‘œ μ°Έμ‘°ν•˜μ—¬ μ‚¬μš©ν•  수 μžˆλ‹€.


src/main/resources κ²½λ‘œμ— application.properties νŒŒμΌμ„ μƒμ„±ν•œ λ’€ 파일 내에
spring.output.ansi.enabled=ALWAYS 라고 μž…λ ₯ν•΄μ£Όμž.
ANSI λ₯Ό μ§€μ›ν•˜λŠ” Consoleμ—μ„œ output 에 항상 색을 μž…νžˆκ²Œ ν•œλ‹€λŠ” λœ»μ΄λ‹€. 

application.properties
output ansi μ„€μ • ν›„ Spring Boot μ‹€ν–‰ Log μ°½

μ„€μ • ν›„ μ•Œλ‘λ‹¬λ‘ 예쁜 둜그창이 좜λ ₯λ˜λŠ”κ±Έ 확인할 수 μžˆμ—ˆλ‹€πŸ₯³

πŸ‘‰ Nuxt.js

Nuxt.js

Vue ν”„λ ˆμž„μ›Œν¬ 기반 κ°œλ°œν™˜κ²½ ꡬ좕에 도움을 μ£ΌλŠ” ν”„λ ˆμž„μ›Œν¬μ΄λ‹€.
κ°„λ‹¨ν•˜κ²Œ μ–˜κΈ°ν•˜λ©΄ μ„œλ²„μ‚¬μ΄λ“œ λ Œλ”λ§, Store, Router κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” Vue.js ν”„λ ˆμž„μ›Œν¬μ΄λ‹€.

 

npm init nuxt-app <project-name>

Nuxt.js λ₯Ό Installν•˜λŠ”λ°μ—” μ—¬λŸ¬κ°€μ§€ 방법이 μžˆμ§€λ§Œ μ΄λ²ˆμ—” npm λ₯Ό μ΄μš©ν•΄λ³΄κ² λ‹€.
λ‹€λ₯Έ Install 방법은 μ—¬κΈ°μ„œ 확인할 수 μžˆλ‹€.

 

<project-name> 을 "frontend"둜 ν•΄μ„œ μ„€μΉ˜ν•˜λ‹ˆ frontend 폴더 내에 Nuxt.js ν”„λ‘œμ νŠΈκ°€ μƒμ„±λœκ±Έ 확인할 수 μžˆμ—ˆλ‹€.

 

cd <project-name>
npm run dev

μœ„ λͺ…λ Ήμ–΄λ‘œ Nuxt.jsλ₯Ό μ‹œμž‘ν•  수 μžˆλ‹€.
이둜써 Nuxt.js μ„€μΉ˜κ°€ 끝났닀!πŸŽ‰

Spring Boot와 Nuxt.js μ„€μΉ˜λ₯Ό λͺ¨λ‘ λλƒˆμœΌλ‹ˆ, 이제 두 μ„œλ²„λ₯Ό μ—°λ™μ‹œν‚¬ μ°¨λ‘€λ‹€.
ν”„λ‘ νŠΈμ—”λ“œ μ„œλ²„μ™€ λ°±μ—”λ“œ μ„œλ²„λŠ” μ–΄λ–»κ²Œ μ—°κ²°λ˜λŠ”κ±ΈκΉŒ?

β›³ Spring Boot, Nuxt.js 연동

ν”„λ‘ νŠΈμ—”λ“œ μ„œλ²„μ™€ λ°±μ—”λ“œ μ„œλ²„λ₯Ό μ—°κ²°ν•˜λ €λ©΄ 우린 ν”„λ‘μ‹œ(Proxy) μ„œλ²„λ₯Ό μ΄ν•΄ν•΄μ•Όν•œλ‹€.

 

ν”„λ‘μ‹œ(Proxy) μ„œλ²„

"Proxy"λŠ” 단어 κ·ΈλŒ€λ‘œ ν•΄μ„ν•˜λ©΄ "λŒ€λ¦¬"λΌλŠ” λœ»μ„ 가지고 μžˆλ‹€.
ν”„λ‘μ‹œ μ„œλ²„λŠ” μ„œλ‘œ 연결점이 μ—†κ±°λ‚˜ λ³΄μ•ˆμƒμ˜ 이유둜 직접 톡신할 수 μ—†λŠ” μ™ΈλΆ€ λ„€νŠΈμ›Œν¬λ“€μ„
κ°„μ ‘μ μœΌλ‘œ μ—°κ²°μ‹œν‚€λŠ” 쀑계인 역할을 ν•œλ‹€.

즉, Spring Boot(λ°±μ—”λ“œ μ„œλ²„)와 Nuxt.js(ν”„λ‘ νŠΈμ—”λ“œ μ„œλ²„)λ₯Ό μ—°κ²°ν•˜κΈ° μœ„ν•΄μ„  쀑계인인 Proxyκ°€ ν•„μš”ν•˜λ‹€λŠ” 뜻.

μ‚¬μš©μžκ°€ ν”„λ‘ νŠΈ μ—”λ“œ μ„œλ²„λ‘œ μ ‘κ·Όν•΄μ„œ λ¦¬μ†ŒμŠ€λ₯Ό μš”μ²­ν•˜λ©΄, ν”„λ‘μ‹œλŠ” 이 μš”μ²­μ„ λ°±μ—”λ“œλ‘œ μ—°κ²°μ‹œμΌœ μš”μ²­μ„ μ „λ‹¬ν•œλ‹€.
그럼 이제 κ·Έ ν”„λ‘μ‹œλ₯Ό 섀정해보도둝 ν•˜μž.

 

Spring Boot μ„œλ²„ μ½˜μ†”

Spring Boot λ₯Ό μ‹œμž‘ν•  λ•Œ, ν†°μΊ£ μ„œλ²„κ°€ 포트 8080으둜 μ‹€ν–‰λ˜λŠ” κ±Έ 확인할 수 μžˆμ—ˆλ‹€.

 

Nuxt.js μ„œλ²„ μ½˜μ†”

Nuxt.jsλŠ” port 번호 3000 으둜 싀행이 λ˜μ—ˆλ‹€.


πŸš€ μ°Έκ³ , 포트 번호 μˆ˜μ •λ²•
Nuxt.js λŠ” config 파일 λ˜λŠ” package.json νŒŒμΌμ„ 톡해, Spring BootλŠ” application.properties μ„€μ •νŒŒμΌλ‘œ μˆ˜μ •ν•  수 μžˆλ‹€.

  • Nuxt.js
    1. nuxt.config.js
      • 루트경둜의 nuxt.config.js에 μ•„λž˜μ™€ 같이 μž…λ ₯.
      module.exports = {
      //...
        server: {
          port: 3001, // λ°”κΏ€ 포트번호 μž…λ ₯
        },
      };
      • nuxt.config.js λŠ” Nuxt.js의 μ›ΉνŒ© μ„€μ • νŒŒμΌμ΄λ‹€.
    2. package.json
      • package.json의 scripts 뢀뢄에 --port <port> λ₯Ό μΆ”κ°€.
      "scripts": {
          "dev": "nuxt --port 3001",
          "build": "nuxt build",
          "start": "nuxt start --port 3001",
          "generate": "nuxt generate"
        }
  • Spring Boot
    1. application.properties
      • src/main/resources ν•˜μœ„μ— application.properties νŒŒμΌμ„ μƒμ„±ν•˜κ³  server.port = 8081 을 μž…λ ₯.
      • application.properties νŒŒμΌμ€ μ™ΈλΆ€ μ„€μ • 파일둜, μ• ν”Œλ¦¬μΌ€μ΄μ…˜(ν”„λ‘œμ νŠΈ)μ—μ„œ μ‚¬μš©ν•˜λŠ” μ—¬λŸ¬κ°€μ§€ μ„€μ • 값듀을 ν‚€, κ°’ ν˜•μ‹μœΌλ‘œ μ €μž₯해두면 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ λ°–μ΄λ‚˜ μ•ˆμ—μ„œ μ°Έμ‘°ν•΄ μ“Έ 수 μžˆλ‹€.
      • application.propertiesνŒŒμΌμ€ Spring Bootκ°€ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ ꡬ동할 λ•Œ μžλ™μœΌλ‘œ λ‘œλ”©ν•˜λŠ” νŒŒμΌμ΄λ‹€.
    2. Intellij IDE

Edit Configurations...

  • μƒλ‹¨μ˜ Edit Configuration 을 ν΄λ¦­ν•˜κ±°λ‚˜ alt + shift + f10 단좕킀λ₯Ό 톡해 Run/Debug Configurations 창을 μ—΄μž.

  • Environment variables μž…λ ₯칸에 server.port=8081 을 μž…λ ₯ν•˜λ©΄ 포트λ₯Ό μ„€μ •ν•  수 μžˆλ‹€.

 

3000 ν¬νŠΈμ—μ„œ /api둜 μ‹œμž‘ν•˜λŠ” μš”μ²­μ΄ λ°œμƒν•˜λ©΄ 8080 포트둜 연결을 μ‹œν‚€λŠ” ν”„λ‘μ‹œλ₯Ό μ„€μ •ν•΄λ³΄μž.

Vue.js에선 vue.config.js νŒŒμΌμ—μ„œ devServer.proxy (개발 λͺ¨λ“œμ—μ„œ ν”„λ‘μ‹œ μ„€μ •) λ₯Ό 톡해 ν”„λ‘μ‹œλ₯Ό μ„€μ •ν•  수 μžˆλ‹€.
ν•˜μ§€λ§Œ Nuxt.jsλŠ” μ„œλ²„ μ‚¬μ΄λ“œ λ Œλ”λ§μ„ μ§€μ›ν•˜λŠ” Universal μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄κΈ° λ•Œλ¬Έμ—
Vue.js처럼 μ›ΉνŒ© devServer.proxyλ₯Ό μ‚¬μš©ν•  수 μ—†λ‹€.
μ΄μœ λŠ”, Nuxt.jsλŠ” ν”„λ‘œλ•μ…˜(production - 배포용)μ—μ„œμ‘°μ°¨ ν”„λ‘μ‹œκ°€ ν•„μš”ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.
초기 μš”μ²­μ΄ 처리되고 앱이 μ„œλ²„ μΈ‘μ—μ„œ λ Œλ”λ§λ  λ•Œ λΈŒλΌμš°μ €κ°€ μ•„λ‹Œ μ„œλ²„κ°€ ν˜ΈμΆœμ„ μˆ˜ν–‰ν•˜λ―€λ‘œ λͺ¨λ“  API μš”μ²­μ€ ν”„λ‘μ‹œκ°€ λ˜μ–΄μ•Ό ν•œλ‹€.

 

npm i @nuxtjs/axios 
npm i @nuxtjs/proxy

λ”°λΌμ„œ @nuxtjs/proxy λͺ¨λ“ˆμ„ λ”°λ‘œ μ„€μΉ˜ν•΄ 주자
(api μš”μ²­μ„ μœ„ν•œ axios도 μ„€μΉ˜)

 

module.exports = {
    modules: [
        '@nuxtjs/axios',
        '@nuxtjs/proxy'
    ],
    axios: {
        proxy: true
    },
    proxy: {
        '/api': {
            target: 'http://localhost:8080/',
            changeOrigin: true // cross origin ν—ˆμš© 
        }
    }
}

nuxt.config.js

 

λͺ¨λ“ˆμ΄ μ„€μΉ˜λ˜κ³  λ‚˜λ©΄ nuxt.config.js νŒŒμΌμ— μœ„ μ½”λ“œλ₯Ό μΆ”κ°€.
/api 둜 μ‹œμž‘ν•˜λŠ” μš”μ²­μ΄ λ“€μ–΄μ˜€λ©΄ target, 즉 8080 μ„œλ²„λ‘œ μš”μ²­μ„ μ „λ‹¬ν•œλ‹€λŠ” λœ»μ΄λ‹€.
changeOrigin: trueλŠ” "Cross Origin"을 ν—ˆμš©ν•œλ‹€λŠ” 뜻으둜, λ§κ·ΈλŒ€λ‘œ "ꡐ차 좜처"λ₯Ό ν—ˆμš©ν•œλ‹€λŠ” 것을 μ˜λ―Έν•œλ‹€.
"ꡐ차 좜처" λΌλŠ” 말이 쑰금 μƒμ†Œν•  수 μžˆλŠ”λ°, μ‰½κ²Œ ν’€μ–΄ λ§ν•˜λ©΄ "λ‹€λ₯Έ 좜처"이닀. μ—¬κΈ°μ„œ μΆœμ²˜λŠ” μ„œλ‘œ λ‹€λ₯Έ 포트λ₯Ό μ˜λ―Έν•œλ‹€.
"μ„œλ‘œ λ‹€λ₯Έ 좜처의 λ¦¬μ†ŒμŠ€ κ³΅μœ ν•œλ‹€" 우린 이것을 CORS(Cross-Origin Resource Sharing) 이라고 λΆ€λ₯Έλ‹€.

 

ν”„λ‘μ‹œ 섀정이 μ™„λ£Œ λ˜μ—ˆλ‹€!πŸŽ‰
이제 μ œλŒ€λ‘œ μ—°κ²°λ˜μ—ˆλŠ”μ§€ ν™•μΈν•΄λ³΄μž.

 

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloWorld {

    @GetMapping(value="/helloWorld")
    public String helloWorld() {
        return "Hello World!";
    }
}

HelloWorld.java

 

src/main/java/νŒ¨ν‚€μ§€ 내에 HelloWorld.javaλΌλŠ” ν…ŒμŠ€νŠΈ νŒŒμΌμ„ λ§Œλ“€κ³  μœ„μ™€ 같이 μž…λ ₯ν–ˆλ‹€.

 

localhost:3000/api/helloWorld 경둜의 λΈŒλΌμš°μ €

http://localhost:3000/api/helloWorld 으둜 μ ‘κ·Όν•˜μž "Hello World!" κ°€ 좜λ ₯된걸 확인할 수 μžˆμ—ˆλ‹€.
3000 포트둜 μš”μ²­ν–ˆμ§€λ§Œ ν”„λ‘μ‹œλ₯Ό 톡해 8080 포트의 κ²°κ³Όκ°€ λ‚˜μ™”λ‹€.

 

이번 ν¬μŠ€νŒ…μ˜ λͺ©ν‘œμ˜€λ˜ Spring Boot와 Nuxt.js 연동을 μ™„λ£Œν•΄λ³΄μ•˜λ‹€.πŸ™Œ
λΆ€μ‘±ν•œ κΈ€μ΄μ§€λ§Œ λ΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€.

ν”Όλ“œλ°±μ€ μ–Έμ œλ‚˜ ν™˜μ˜!

 

더보기

 

webpack.js.org/configuration/dev-server/#devserverproxy

 

DevServer | webpack

webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset.

webpack.js.org

stackoverflow.com/questions/56165709/nuxt-js-spa-devserver-settings

 

Nuxt.js SPA devserver settings

I have configured devserver in vue-config.js before like this: devServer: { proxy: { "/api/*": { target: "http://localhost:3001", secure: false } } } In nuxt-SPA this dont...

stackoverflow.com

yunyoung1819.tistory.com/9

 

ν”„λ‘μ‹œ μ„œλ²„(Proxy Server)

ν”„λ‘μ‹œ μ„œλ²„(Proxy Server)λž€? ν”„λ‘μ‹œ μ„œλ²„(Proxy Server)λž€ μ‹œμŠ€ν…œμ— 방화벽을 가지고 μžˆλŠ” 경우 μ™ΈλΆ€μ™€μ˜ 톡신을 μœ„ν•΄ λ§Œλ“€μ–΄ 놓은 μ„œλ²„ - ν”„λ‘μ‹œ(Proxy)λž€ μ›λž˜ 'λŒ€λ¦¬' λΌλŠ” 의미λ₯Ό 가지고 있음. - 프둝

yunyoung1819.tistory.com

evan-moon.github.io/2020/05/21/about-cors/

 

CORSλŠ” μ™œ μ΄λ ‡κ²Œ 우리λ₯Ό νž˜λ“€κ²Œ ν•˜λŠ”κ±ΈκΉŒ?

이번 ν¬μŠ€νŒ…μ—μ„œλŠ” μ›Ή 개발자라면 ν•œλ²ˆμ―€μ€ μ–»μ–΄λ§žμ•„ 봀을 λ²•ν•œ 정책에 λŒ€ν•œ 이야기λ₯Ό 해보렀고 ν•œλ‹€. 사싀 μ›Ή κ°œλ°œμ„ ν•˜λ‹€λ³΄λ©΄ CORS μ •μ±… μœ„λ°˜μœΌλ‘œ 인해 μ—λŸ¬κ°€ λ°œμƒν•˜λŠ” 상황은 ꡉμž₯히 ν”ν•΄μ„œ

evan-moon.github.io

jeong-pro.tistory.com/168

 

메이븐(Maven)은 μ•Œκ³  μŠ€ν”„λ§(Spring)을 μ“°λŠ”κ°€? (pom.xml λΆ„μ„ν•˜λ©° κ°€λ³κ²Œ μ •λ¦¬ν•˜λŠ” λΉŒλ“œ 툴, Maven)

메이븐(Maven)은 μ•Œκ³  μŠ€ν”„λ§(Spring)을 μ“°λŠ”κ°€? 제λͺ©κ³Ό 같은 μ§ˆλ¬Έμ— ν•„μžλŠ” μ œλŒ€λ‘œ λ‹΅λ³€ν•  수 μ—†μ—ˆλ‹€. λ‹¨μˆœν•˜κ²ŒλŠ” maven repositoryμ—μ„œ dependency μΆ”κ°€ν•΄μ„œ ν•„μš”ν•œ 라이브러리 μ“°λŠ” 정도? 무엇을 μœ„ν•΄

jeong-pro.tistory.com

goddaehee.tistory.com/199

 

[Maven] Maven μ΄λž€? (μ •μ˜, 예제)

[Maven] 메이븐 μ΄λž€? (μ •μ˜, 예제) μ•ˆλ…•ν•˜μ„Έμš”. κ°“λŒ€ν¬ μž…λ‹ˆλ‹€. 이번 ν¬μŠ€νŒ…μ€ [ 메이븐 μ•Œμ•„λ³΄κΈ° ] μž…λ‹ˆλ‹€. : ) 1. λΉŒλ“œ (Build) #1 λΉŒλ“œλž€?  - μ†ŒμŠ€μ½”λ“œ νŒŒμΌμ„ μ»΄ν“¨ν„°μ—μ„œ μ‹€ν–‰ν•  μˆ˜ μžˆλŠ” λ…립

goddaehee.tistory.com

ko.nuxtjs.org/

 

Nuxt.js - Vue.js ν”„λ ˆμž„μ›Œν¬

Nuxt.jsλŠ” Vue.js μ• ν”Œλ¦¬μΌ€μ΄μ…˜ κ°œλ°œμ„ 즐겁게 ν•˜λŠ” 데 ν•„μš”ν•œ λͺ¨λ“  ꡬ성을 사전 μ„€μ •ν•©λ‹ˆλ‹€. Nuxt.jsλŠ” λ²”μš©, SPA 및 정적 생성 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

ko.nuxtjs.org

github.com/kimmy100b/spring-boot-vue/wiki/HowToCreateProject

 

kimmy100b/spring-boot-vue

Spring Boot와 Vue의 ν”„λ‘œμ νŠΈ. Contribute to kimmy100b/spring-boot-vue development by creating an account on GitHub.

github.com

'Spring > Spring Boot' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

Spring Boot + MyBatis + MySQL 연동 방법  (7) 2021.06.03

Seize the day!

Spring MVC | Spring Boot | Spring Security | Mysql | Oracle | PostgreSQL | Vue.js | Nuxt.js | React.js | TypeScript | JSP | Frontend | Backend | Full Stack | μžκΈ°κ³„λ°œ | 미라클 λͺ¨λ‹ | 일상