Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[NodeJS] React, Node ์—ฐ๊ฒฐํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿง‘‍๐Ÿ’ป Web/NodeJS

[NodeJS] React, Node ์—ฐ๊ฒฐํ•˜๊ธฐ

Lennon 2022. 6. 19. 17:29
728x90
๋ฐ˜์‘ํ˜•

CRA๋กœ ๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ๋‹ค.

ํ•ด๋‹น ํด๋”๋กœ ๋“ค์–ด๊ฐ€ ๋จผ์ € ์˜์กด์„ฑ์„ ์„ค์น˜ํ•ด๋ณด์ž

npm install express                 // express ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ! 
npm install concurrently        // package.json์—์„œ ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ๋™์‹œ์— ์‹คํ–‰์‹œํ‚ด
npm install cors                      // cors
npm install http-proxy-middleware                // ํด๋ผ์ด์–ธํŠธ์™€ ์—ฐ๋™
npm install nodemon             // ์ƒˆ๋กœ๊ณ ์นจํ•ด๋„ ์ž˜ ์ ์šฉ๋˜๊ฒŒ!

 

๋‚ด ํ”„๋กœ์ ํŠธ ๊ธฐ์ค€ ํด๋”๊ตฌ์กฐ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

server ํด๋”๋ฅผ ๋งŒ๋“ค์–ด route๋ผ๋Š” ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  ์•ˆ์— index.js๋ฅผ ๋„ฃ์ž.
๊ทธ ํ›„ serverํด๋” ์ „์—ญ์— app.js๋ฅผ ๋งŒ๋“ค์ž!

 

ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์—ฐ๋™์„ ์œ„ํ•œ proxy์„ค์ •์„ ์œ„ํ•ด srcํด๋” ๋‚ด๋ถ€์— setProxy.js๋ฅผ ์ƒ์„ฑํ•˜์ž.

 

index.js

// server/route/index.js

const express = require("express");
const router = express.Router();

router.get("/", (req, res) => {
  res.send("hello World");
});

module.exports = router;

app.js

const express = require("express");
const app = express();
const cors = require("cors");
const basic = require("./route/index");

const port = process.env.PORT || 3001;

app.use(cors());
app.use(bodyParser.json());
app.use("/", basic);

app.listen(port, () => {
  console.log(`express is running on ${port}`);
});

setProxy.js

const proxy = require("http-proxy-middleware");

module.exports = (app) => {
  app.use(proxy("/", { target: "http://localhost:3001/" }));
};

 

์ด์ œ package.json์œผ๋กœ ์ด๋™ํ•ด์„œ concurrently๋ฅผ ์‚ฌ์šฉํ•ด script๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋„๋ก ํ•˜์ž.

npm run dev

๋กœ ์‹คํ–‰ํ•ด๋ณด๋ฉด ํด๋ผ์ด์–ธํŠธ์™€ ์—ฐ๋™๋˜์–ด ์ž˜ ์‹คํ–‰๋˜๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ axios๋ฅผ ์‚ฌ์šฉํ•ด ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ํ†ต์‹ ์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

728x90
๋ฐ˜์‘ํ˜•
Comments