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

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

[express] typescript๋กœ next.js ์™ธ๋ถ€์„œ๋ฒ„ ๋งŒ๋“ค๊ธฐ

728x90
๋ฐ˜์‘ํ˜•

๐ŸŸก next.js ํ™˜๊ฒฝ ์„ค์ •

npx create-next-app servertest

์—ฐ๋™ํ•  nextjs front๋ฅผ ํ•˜๋‚˜ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋‘”๋‹ค.

๐Ÿ”ด package ์„ค์น˜ (server ํด๋” ์ƒˆ๋กœ์ƒ์„ฑ)

//front nextjs ํด๋”์™€ ๋‹ค๋ฅธ ๋…๋ฆฝ๋œ ํด๋”๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ›„
npm init -y
npm install express 
npm install typescript @types/express
npm install tsnode @types/tsnode

๐ŸŸฃ index.ts ํŒŒ์ผ ์ž‘์„ฑ

express ์„œ๋ฒ„๋ฅผ ์„ค์ •ํ•˜๊ณ , ๊ตฌ์„ฑํ•˜๋Š” logic๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

import express, { Request, Response } from "express";
import cors from "cors";
const app = express();
app.use(cors());
// Define your Express routes, middleware, and other server configurations here
app.get("/api/data", (req: Request, res: Response) => {
  // Handle your Express route logic here
  res.json({ message: "Hello from Express server!" });
});

app.post("/api/click", (req: Request, res: Response) => {
  // Handle your Express route logic here
  res.send({ msg: "ํ†ต์‹ ์™„๋ฃŒ!!" });
});

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

๐ŸŸฃ front code ์ˆ˜์ •

import Head from "next/head";
import Image from "next/image";
import { Inter } from "next/font/google";
import styles from "@/styles/Home.module.css";
// import fetch from "isomorphic-fetch";
// import getConfig from "next/config";
import { useEffect, useState } from "react";
// const { serverRuntimeConfig } = getConfig();
import axios from "axios";

const inter = Inter({ subsets: ["latin"] });

export default function Home() {
  const [test, setTest] = useState();
  const getData = async () => {
    const result = await axios.get(`http://localhost:5000/api/data`);

    console.log("์˜คํ”ˆ");
    console.log(result.msg);
  };
  const click = async () => {
    const result = (await axios.post(`http://localhost:5000/api/click`)).data;
    console.log(result);
    setTest(result);
  };
  useEffect(() => {
    // console.log("์˜คํ”ˆ2", serverRuntimeConfig.expressServerUrl);
    getData();
  }, []);

  return (
    <>
      <button
        onClick={() => {
          click();
        }}
      >
        ํ†ต์‹ ๋ฐ›์•„๋ผ!
      </button>
      {test ? test : "ํ†ต์‹ ์ „"}
    </>
  );
}

๊ธฐ๋ณธ next.js ์ƒ์„ฑ๋œ front์—์„œ ์•ˆ์˜ ์ฝ”๋“œ๋งŒ ์ง€์šฐ๊ณ , ์ฒ˜์Œ ์ ‘์†ํ–ˆ์„๋•Œ ํ†ต์‹  ํ™•์ธํ•˜๋Š” get ํ†ต์‹ ๊ณผ, click ํ–ˆ์„ ๋•Œ์˜ ๋ฐ˜์‘์„ ๋ณด๊ธฐ์œ„ํ•œ post ํ†ต์‹  2๊ฐ€์ง€๋ฅผ ์ค€๋น„ํ–ˆ๋‹ค.

 

๐ŸŸ  next.js ํŽ˜์ด์ง€์—์„œ express ์„œ๋ฒ„๋ฅผ ์‚ฌ์šฉ

์„œ๋ฒ„ bash์—์„œ๋Š” ts-node index๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์„œ๋ฒ„๊ฐ€ ์ž˜ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

front bash ์—์„œ๋Š” npm run dev๋ฅผ ์ž…๋ ฅํ•˜์—ฌ next๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

๐ŸŸข ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ

get ํ†ต์‹ ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๊ณ  , message๋ฅผ return ๋ฐ›์•˜์Œ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ onclick ์ด ํด๋ฆญ์ „์ด๋ฏ€๋กœ ํ†ต์‹ ์ „์ด ํ‘œ๊ธฐ๋œ๋‹ค.

ํด๋ฆญํ•˜๊ณ , ์ƒํƒœ๋ณ€ํ™”๋Š” ์•„๋ž˜์˜ ๋™์˜์ƒ์— ์žˆ๋‹ค.

 

๐Ÿ”ต

728x90
๋ฐ˜์‘ํ˜•


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