-
node js ์ค์น
-
SpringProjectName/src/main/ ๊ฒฝ๋ก๋ก ์ ๊ทผ ํ react ์ค์น
cd src/main
npx create-react-app frontend # npx create-reeact {ํ๋ก์ ํธ๋ช
}- ๋ง๋ ๋ฆฌ์กํธ ํ๋ก์ ํธ ํด๋๋ก ์ด๋ํ์ฌ npm ์คํ
cd frontend # cd {ํ๋ก์ ํธ๋ช
}
npm start- ์ ํฐ๋ฏธ๋ ์ฐฝ ์ด๊ณ ์์ ํ๋ ๊ฒฝ๋ก๋ก ์ด๋ ํ ์๋ ๋ช ๋ น์ด ์คํ
npm install
npm run-script build
npm run eject # ์๋ฌ ๋ฐ์ ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ถ git pushํ ์ํ์ธ์ง ํ์ธ-
[๋ฆฌ์กํธ PROJECT NAME]/config/paths.js ํ์ผ์์ buildPath ๋ณ์ build -> build/static
-
[๋ฆฌ์กํธ PROJECT NAME] ํด๋์์ axios๋ฅผ ์ค์น
npm install axios --save
npm install http-proxy-middleware --save- [๋ฆฌ์กํธ PROJECT NAME]/src/App.js์ ํด๋น ์ฝ๋ ์ถ๊ฐ
import React, {useEffect, useState} from 'react';
import axios from 'axios';
function App() {
const [hello, setHello] = useState('')
useEffect(() => {
axios.get('/api/hello')
.then(response => setHello(response.data))
.catch(error => console.log(error))
}, []);
return (
<div>
๋ฐฑ์๋์์ ๊ฐ์ ธ์จ ๋ฐ์ดํฐ์
๋๋ค : {hello}
</div>
);
}
export default App;-
[๋ฆฌ์กํธ PROJECT NAME]/pakage.json์ "proxy": "http://localhost:8080" ์ถ๊ฐ
-
src/main/java/com.[springboot ํ๋ก์ ํธ]์์ ํจํค์ง ์์ฑ ํ HelloWorldController.java ํ์ผ์ ์์ฑ
package com.demogroup.demoweb.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/api/hello")
public String test() {
return "Hello, world!";
}
}์ฌ๊ธฐ๊น์ง ์งํ์ localhost:3000์์๋ "๋ฐฑ์๋์์ ๊ฐ์ ธ์จ ๋ฐ์ดํฐ์ ๋๋ค : hello world!"๊ฐ, localhost:8080์์๋ "hello world!"๊ฐ ์ถ๋ ฅ๋์ด์ผ ํ๋ค. (spring boot๋ project run ์ดํ react๋ npm start)
- SpringBoot ํ๋ก์ ํธ์ build.gradle๋ก ์ด๋ํด์ dependencies{ ... }์ test{ ... } ์ฌ์ด์ ์๋ ์์ค ์ฒจ๋ถ
def webappDir = "$projectDir/src/main/[PROJECTNAME]"
sourceSets {
main {
resources {
srcDirs = ["$webappDir/build", "$projectDir/src/main/resources"]
}
}
}
processResources {
dependsOn "buildReact"
}
task buildReact(type: Exec) {
dependsOn "installReact"
workingDir "$webappDir"
inputs.dir "$webappDir"
group = BasePlugin.BUILD_GROUP
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {
commandLine "npm.cmd", "run-script", "build"
} else {
commandLine "npm", "run-script", "build"
}
}
task installReact(type: Exec) {
workingDir "$webappDir"
inputs.dir "$webappDir"
group = BasePlugin.BUILD_GROUP
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {
commandLine "npm.cmd", "audit", "fix"
commandLine 'npm.cmd', 'install'
} else {
commandLine "npm", "audit", "fix"
commandLine 'npm', 'install'
}
}
task copyReactBuildFiles(type: Copy) {
dependsOn "buildReact"
from "$frontendDir/build"
into "$projectDir/src/main/resources/static"
}- ํ ๋๋ ํ ๋ฆฌ๋ก ๋น ์ ธ๋์ ๋น๋๋ฅผ ์คํ(ํ ๋ฒ ์คํ ์ดํ ๋ค์ ์คํ ํ๋ค๋ฉด [๋ฆฌ์กํธ PROJECT NAME]/build ํด๋์ [SpringProjectName]/src/main/resources/static ํด๋ ์ญ์ ํ ์ฌ์คํ)
./gradlew build-
๋น๋ ์๋ฃ ํ [SpringProjectName]/build/libs๋ก ์ด๋
-
ํด๋ ์์ ์๋ jar ํ์ผ ์คํ(java -jar FILENAME)
-
Localhost:8080 ์ ์ํด์ Reactํ๋ฉด ์ถ๋ ฅ๋๋ฉด ์ฑ๊ณต!
-
package.json, package-lock.json, src ํด๋ ๋ฎ์ด์ฐ๋ฉด ๋จ
-
package.json, package-lock.json์ name ๋ถ๋ถ์ ์์ ์ ๋ฆฌ์กํธ ํ๋ก์ ํธ ์ด๋ฆ์ผ๋ก ๋ณ๊ฒฝ
-
[๋ฆฌ์กํธ PROJECT NAME]/build ํด๋์ [SpringProjectName]/src/main/resources/static ํด๋ ์ญ์ ํ ./gradlew build ์คํ
-
๋น๋ ์๋ฃ ํ [SpringProjectName]/build/libs๋ก ์ด๋
-
ํด๋ ์์ ์๋ jar ํ์ผ ์คํ(java -jar FILENAME)
-
์ดํ ํ์ํ ๋ชจ๋์ด ์๋ค๋ ์๋ฌ ๋ฐ์์ ์๋ ํ์์ผ๋ก ๋ค์ด๋ก๋
npm install [๋ชจ๋์ด๋ฆ] --save- ERESOLVE could not resolve ์๋ฌ ๋ฐ์์ [๋ฆฌ์กํธ PROJECT NAME] ํด๋ ์ด๋ ํ ์๋ ๋ช ๋ น์ด ์คํ ํ ๋ค์ 3๋ฒ ๋ฐ๋ณต
npm config set legacy-peer-deps true
npm i- ํ ๋ฒ ์ฑ๊ณตํ ์ดํ์๋ ๊ทธ๋ฅ application.class run ํ๋ฉด ๋ณ๋์ฌํญ ์๋ ๋ฐ์๋จ
build.gradle ํ์ผ์ ์๋ ์ฝ๋ ์ถ๊ฐ
tasks {
processResources {
duplicatesStrategy = org.gradle.api.file.DuplicatesStrategy.INCLUDE
}
}