๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[tpyescript] server ๊ตฌ์ถ•

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ”ต tpyesrcipt ๋กœ server ์—ฌ๋Š” ๋ฐฉ๋ฒ•

server ํด๋”๋ฅผ ๋งŒ๋“ค์–ด ๋†“๊ณ  ์•„๋ž˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ผํ•œ๋‹ค.

๐ŸŸข terminal ์„ธํŒ…

npm init -y
npm i express ts-node typescript dotenv nodemon cors
npm i @types/express @types/node @types/cors

์ถ”ํ›„ next js (port : 3000) ๊ณผ ์—ฐ๋™ํ•˜๊ธฐ ์œ„ํ•ด cors๋„ ์„ค์น˜ํ•ด์ค€๋‹ค.

- express : web application ๊ตฌ์ถ• ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹จ์ˆœํ™” ํ•˜๋Š” node.js ์šฉ ์›น ํ”„๋ ˆ์ž„์›Œํฌ -> ์‰ฝ๊ฒŒ ์„œ๋ฒ„๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“  class ์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ง‘ํ•ฉ์ฒด

- dotenv : .env ํŒŒ์ผ์—์„œ application ์œผ๋กœ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํŒจํ‚ค์ง€. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž๊ฒฉ ์ฆ๋ช… ๋ฐ API ํ‚ค์™€ ๊ฐ™์€ ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ์ฝ”๋“œ์—์„œ ์‰ฝ๊ฒŒ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

- nodemon : ์ฝ”๋“œ ๋ณ€๊ฒฝ์„ ์ž๋™์œผ๋กœ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•˜๊ณ  ๋ณ€๊ฒฝ ์‹œ ์ž๋™์œผ๋กœ ๋‹ค์‹œ ์‹œ์ž‘ํ•ด์ฃผ๋Š” ๋„๊ตฌ. ์ˆ˜๋™์œผ๋กœ ์„œ๋ฒ„๋ฅผ ๊ป๋‹ค ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋Š” ์ˆ˜๊ณ ๋ฅผ ๋œ ์ˆ˜ ์žˆ๋‹ค.

 

๐ŸŸ  index.ts ์ƒ์„ฑ (server ๋ฉ”์ธ ํŒŒ์ผ)

import express, { Request, Response } from "express";
import cors from "cors";
const app = express();
const port = process.env.PORT || 3000;
app.use(cors());
// Define your Express routes, middleware, and other server configurations here

app.get("/", async (req, res) => {
  console.log("12342424123");
});

// Start the server
app.listen(port, () => {
  console.log("Express server is running on http://localhost:" + port);
});

๐ŸŸฃ ์‹คํ–‰๊ฒฐ๊ณผ ํ™”๋ฉด

- nodemon ์„ ์„ค์น˜ํ•  ๋•Œ ์ „์—ญ์œผ๋กœ ์„ค์น˜ํ–ˆ์„ ๋•Œ

npm i -g nodemon

//์‹คํ–‰๋ช…๋ น์–ด
nodemon ํŒŒ์ผ๋ช…

- nodemon ์„ ์ง€์—ญ์œผ๋กœ ์„ค์น˜ํ–ˆ์„ ๋•Œ 

npm i nodemon

// ์‹คํ–‰๋ช…๋ น์–ด
npx nodemon ํŒŒ์ผ๋ช…

(nodemon ์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค.)

 

728x90
๋ฐ˜์‘ํ˜•


Calendar
ยซ   2024/09   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Visits
Today
Yesterday