๐ต 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 ์ด ์๋๋ผ ๋ค๋ฅธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ง์ฐฌ๊ฐ์ง์ด๋ค.)