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

πŸ’¬/γ…γ……γ…Œγ…‹γ…γ…… μ±Œλ¦°μ§€

40일차

🦝

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

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 

 

https://s3.console.aws.amazon.com/s3/buckets?region=us-east-1

 

s3.console.aws.amazon.com

 

  • 버킷 μ •μ±… νŽΈμ§‘
{
	"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: 

 

https://us-east-1.console.aws.amazon.com/ec2/v2/home?region=us-east-1#Home:

 

us-east-1.console.aws.amazon.com

 

https://github.com/nvm-sh/nvm

 

GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions - GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active nod...

github.com

 

 

 πŸ”— 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

 

ReactDOMServer – React

A JavaScript library for building user interfaces

ko.reactjs.org

 

// ./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