π¦
40μΌμ°¨
Part 10. React λ‘ μΌνλͺ° λ§λ€κΈ° (React κΈ°λ³Έ)
Ch 10. Deploy React App
Ch 11. React λ‘ λ§λλ μΌνλͺ° νλ‘μ νΈ
Ch 10. Deploy React App
π SPA νλ‘μ νΈ λ°°ν¬ μ΄ν΄νκΈ°
- λͺ¨λ μμ²μ μλ²μ νκ³ λ°μμ€λ ννκ° μλ
- λΌμ°ν κ²½λ‘μ μκ΄μμ΄ λ¦¬μ‘νΈ μ±μ λ°μ μ€ν
- λΌμ°ν μ λ°μμ¨ λ¦¬μ‘νΈ μ±μ μ€ν ν, μ μ©
- static νμΌμ μ μΈν λͺ¨λ μμ²μ index.html λ‘ μλ΅ν΄ μ£Όλλ‘ μμ
$ nvm install 10.16.3
$ npm ci
$ npm run build
π serve ν¨ν€μ§λ‘ React Web App λ°°ν¬νκΈ°
$ npm install serve -g
$ serve -s build # -s μ΅μ
μ μ΄λ€ λΌμ°ν
μΌλ‘ μμ²ν΄λ index.html μ μλ΅νλλ‘ νλ€.
π AWS S3 μ React Web App λ°°ν¬νκΈ°
https://s3.console.aws.amazon.com/s3/buckets?region=us-east-1
- λ²ν· μ μ± νΈμ§
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::react-web-app-test2/*"
]
}
]
}
π NginX λ‘ React Web App λ°°ν¬νκΈ°
https://us-east-1.console.aws.amazon.com/ec2/v2/home?region=us-east-1#Home:
π node.js express λ‘ React Web App λ°°ν¬νκΈ°
$ npm i express
$ node server.js
// server.js
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
// 404
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(9000);
π μλ²μ¬μ΄λ λ λλ§ μ΄ν΄νκΈ°
- Server Side Rendering
- μλ²μμ μλ΅μ κ°μ Έμ¬ λ, κΈ°μ‘΄μ²λΌ static file λ§μ κ°μ Έμ€λ κ²μ΄ μλκ³ , λ¨Όμ μλ²μμ μλ΅ κ°μ λ§λ€μ΄μ λ΄λ €μ£Όκ³ , κ·Έ νμ static νμΌμ λ΄λ €μ€λ€.
- static file μ λ€ λ΄λ €λ°κ³ , 리μ‘νΈ μ±μ λΈλΌμ°μ μμ μ€νν λ€μλ SPA μ²λΌ λμνκ² λλ€.
- React Server Side Rendering
- React Component λ₯Ό λΈλΌμ°μ κ° μλλΌ Node.js μμ μ¬μ©
- ReactDOMServer.renderToString(<App />);
- κ²°κ³Όκ° λ¬Έμμ΄
- μ΄κ²μ μλ΅μΌλ‘ λ΄λ €μ€λ€.
- λΌμ°ν
, 리λμ€ μ κ°μ μ²λ¦¬λ₯Ό μλ²μμ μ§ννκ³ λ΄λ €μ€λ€.
- 볡μ‘, μ΄λ ΅
- JSX κ° ν¬ν¨λ 리μ‘νΈ μ½λλ₯Ό μλ²μμ μ½μ μ μλλ‘ babel μ€μ μ ν΄μΌ νλ€.
https://ko.reactjs.org/docs/react-dom-server.html
// ./server.js
const express = require('express');
const path = require('path');
const ReactDOMServer = require('react-dom/server');
const React = require('react');
const fs = require('fs');
// <div>hello</div>
console.log(
ReactDOMServer.renderToString(React.createElement('div', null, 'hello')),
);
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('./test', (req, res) => {
const ssr = ReactDOMServer.renderToString(
React.createElement('div', null, 'hello'),
);
const indexHtml = fs
.readFileSync(path.join(__dirname, 'build', 'index.html'))
.toString.replace('<div id="root"></div>', `<div id="root">${ssr}</div>`);
console.log(indexHtml);
res.send(indexHtml);
});
// 404
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(9000);
Ch 11. React λ‘ λ§λλ μΌνλͺ° νλ‘μ νΈ
β context API μ Hooks λ₯Ό μ΄μ©ν μ μ μν κ΄λ¦¬
'π¬ > γ γ γ γ γ γ μ±λ¦°μ§' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
41μΌμ°¨ (0) | 2022.03.25 |
---|---|
useMemo μ useCallback μ 무μμ΄ λ€λ₯Έ κ²μΈκ°...... (0) | 2022.03.25 |
39μΌμ°¨ (0) | 2022.03.24 |
38μΌμ°¨ (0) | 2022.03.23 |
37μΌμ°¨ (0) | 2022.03.23 |