πŸŽ‡ Next.js 미듀웨어 μ„€μ •ν•˜κΈ° - 둜그

@JavaPark Β· March 10, 2023 Β· 6 min read

κ°œμš”

SSR μ—μ„œ μ„œλ²„μ—μ„œ 곡톡 둜그λ₯Ό 남기렀고 ν•˜λŠ”λ° μ–΄λ–»κ²Œ μ‹œμž‘ν•΄μ•Ό ν•˜λŠ”μ§€ Chat GPT μ—κ²Œ λ¬Όμ–΄λ³΄μ•˜λ‹€.

1. Next.js λ₯Ό μ΄μš©ν•œ React Application μƒ˜ν”Œ

λ‹€μŒμ€ Next.jsλ₯Ό μ‚¬μš©ν•˜μ—¬ React μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ λ§Œλ“œλŠ” κ°„λ‹¨ν•œ μ˜ˆμ œμž…λ‹ˆλ‹€.

λ¨Όμ €, Next.jsλ₯Ό μ„€μΉ˜ν•΄μ•Όν•©λ‹ˆλ‹€. npm을 μ‚¬μš©ν•˜μ—¬ μ„€μΉ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

npm install next react react-dom

그리고 이제 λ‹€μŒκ³Ό 같이 pages 폴더λ₯Ό λ§Œλ“€κ³  index.js νŒŒμΌμ„ μƒμ„±ν•©λ‹ˆλ‹€.

// pages/index.js

function Home() {
  return <div>Welcome to Next.js!</div>;
}

export default Home;

그리고 λ§ˆμ§€λ§‰μœΌλ‘œ λ‹€μŒκ³Ό 같이 package.json νŒŒμΌμ— λ‹€μŒμ„ μΆ”κ°€ν•˜μ—¬ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μ‹€ν–‰ν•©λ‹ˆλ‹€.

{
  "scripts": {
    "dev": "next dev"
  }
}

이제 ν„°λ―Έλ„μ—μ„œ npm run dev λͺ…령을 μ‹€ν–‰ν•˜κ³  λΈŒλΌμš°μ €μ—μ„œ http://localhost:3000으둜 μ΄λ™ν•˜λ©΄ "Welcome to Next.js!"λΌλŠ” ν…μŠ€νŠΈκ°€ ν‘œμ‹œλ˜λŠ” 것을 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

이것은 Next.jsλ₯Ό μ‚¬μš©ν•˜μ—¬ μƒμ„±λœ 맀우 κ°„λ‹¨ν•œ React μ• ν”Œλ¦¬μΌ€μ΄μ…˜μž…λ‹ˆλ‹€. 이제 Next.jsλ₯Ό 더 깊이 μ΄ν•΄ν•˜κ³  μ»΄ν¬λ„ŒνŠΈ, λΌμš°νŒ…, 데이터 κ°€μ Έμ˜€κΈ° 등을 μΆ”κ°€ν•˜μ—¬ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ 더 λ°œμ „μ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

2. λΌμš°νŒ… μΆ”κ°€

λ‹€μŒμ€ Next.jsμ—μ„œ λΌμš°νŒ…μ„ μΆ”κ°€ν•˜λŠ” λ°©λ²•μž…λ‹ˆλ‹€.

  1. pages 폴더 μ•ˆμ— μƒˆλ‘œμš΄ νŒŒμΌμ„ μƒμ„±ν•˜μ—¬ 라우트λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€. 파일 이름은 라우트 κ²½λ‘œμ™€ μΌμΉ˜ν•΄μ•Όν•©λ‹ˆλ‹€.
// pages/about.js

function About() {
  return <div>About us</div>;
}

export default About;
  1. Link μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜μ—¬ 라우트둜 이동할 수 μžˆλŠ” 링크λ₯Ό λ§Œλ“­λ‹ˆλ‹€.
// pages/index.js

import Link from 'next/link';

function Home() {
  return (
    <div>
      <div>Welcome to Next.js!</div>
      <Link href="/about">
        About us
      </Link>
    </div>
  );
}

export default Home;
  1. next/router λͺ¨λ“ˆμ„ μ‚¬μš©ν•˜μ—¬ ν”„λ‘œκ·Έλž˜λ° λ°©μ‹μœΌλ‘œ λΌμš°νŒ…μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
jsxCopy code
// pages/index.js

import { useRouter } from 'next/router';

function Home() {
  const router = useRouter();

  const handleClick = (e) => {
    e.preventDefault();
    router.push('/about');
  };

  return (
    <div>
      <div>Welcome to Next.js!</div>
      <Link href="/about">
        About us
      </Link>
      <button onClick={handleClick}>Go to about page</button>
    </div>
  );
}

export default Home;

이제 http://localhost:3000μ—μ„œ 클릭 κ°€λŠ₯ν•œ 링크와 λ²„νŠΌμ΄ 있으며, ν΄λ¦­ν•˜λ©΄ http://localhost:3000/about νŽ˜μ΄μ§€λ‘œ μ΄λ™ν•©λ‹ˆλ‹€.

3. μš”μ²­ λ§ˆλ‹€ 둜그λ₯Ό 남기렀면?

Next.jsμ—μ„œ 각 μš”μ²­λ§ˆλ‹€ 둜그λ₯Ό 남기렀면 미듀웨어 ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 미듀웨어 ν•¨μˆ˜λŠ” μš”μ²­κ³Ό 응닡에 λŒ€ν•œ ν•Έλ“€λŸ¬ ν•¨μˆ˜λ‘œ, 이λ₯Ό μ‚¬μš©ν•˜μ—¬ 둜그λ₯Ό 남기고 λ‹€μŒ λ―Έλ“€μ›¨μ–΄λ‘œ μš”μ²­μ„ 전달할 수 μžˆμŠ΅λ‹ˆλ‹€.

λ‹€μŒμ€ Next.jsμ—μ„œ 미듀웨어 ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ 각 μš”μ²­λ§ˆλ‹€ 둜그λ₯Ό λ‚¨κΈ°λŠ” λ°©λ²•μž…λ‹ˆλ‹€.

  1. server.js νŒŒμΌμ„ μƒμ„±ν•©λ‹ˆλ‹€. 이 νŒŒμΌμ€ Next.jsμ—μ„œ μ„œλ²„ μΈ‘ λ Œλ”λ§μ„ κ΅¬μ„±ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€.
// server.js

const express = require('express');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();

  // 미듀웨어 ν•¨μˆ˜λ₯Ό λ“±λ‘ν•©λ‹ˆλ‹€.
  server.use((req, res, next) => {
    console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
    next();
  });

  server.get('*', (req, res) => {
    return handle(req, res);
  });

  server.listen(3000, (err) => {
    if (err) throw err;
    console.log('> Ready on http://localhost:3000');
  });
});
  1. server.js νŒŒμΌμ—μ„œ λ“±λ‘ν•œ 미듀웨어 ν•¨μˆ˜λŠ” 각 μš”μ²­λ§ˆλ‹€ 둜그λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€. console.log λŒ€μ‹  νŒŒμΌμ— 둜그λ₯Ό κΈ°λ‘ν•˜κ±°λ‚˜ 둜그 μ„œλ²„λ‘œ μ „μ†‘ν•˜λŠ” λ“±μ˜ μž‘μ—…μ„ μˆ˜ν–‰ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

이제 Next.js μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μ‹œμž‘ν•˜λ©΄ 각 μš”μ²­μ— λŒ€ν•œ λ‘œκ·Έκ°€ μ½˜μ†”μ— 좜λ ₯λ©λ‹ˆλ‹€. 미듀웨어 ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄ μš”μ²­μ„ κ°€λ‘œμ±„κ³  ν•„μš”ν•œ μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμœΌλ―€λ‘œ, μš”μ²­μ„ κΈ°λ‘ν•˜λŠ” 것 외에도 인증, κΆŒν•œ λΆ€μ—¬, 캐싱 λ“±μ˜ μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

4. 싀행방법

server.js νŒŒμΌμ„ λ§Œλ“  ν›„μ—λŠ” λ‹€μŒ λͺ…λ Ήμ–΄λ₯Ό μ‚¬μš©ν•˜μ—¬ Next.js μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μ‹€ν–‰ν•©λ‹ˆλ‹€.

node server.js

이 λͺ…λ Ήμ–΄λŠ” Node.js둜 server.js νŒŒμΌμ„ μ‹€ν–‰ν•©λ‹ˆλ‹€. server.js νŒŒμΌμ€ Next.jsμ—μ„œ μ„œλ²„ μΈ‘ λ Œλ”λ§μ„ κ΅¬μ„±ν•˜λŠ” 데 μ‚¬μš©λ˜λ―€λ‘œ, 이 νŒŒμΌμ„ μ‹€ν–‰ν•˜λ©΄ Next.js μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μ„œλ²„μ—μ„œ μ‹€ν–‰λ©λ‹ˆλ‹€.

μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μ‹€ν–‰λ˜λ©΄ λΈŒλΌμš°μ €μ—μ„œ http://localhost:3000에 μ ‘μ†ν•˜μ—¬ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ•Œ server.js νŒŒμΌμ—μ„œ λ“±λ‘ν•œ 미듀웨어 ν•¨μˆ˜κ°€ μš”μ²­μ„ κ°€λ‘œμ±„κ³  둜그λ₯Ό 남길 κ²ƒμž…λ‹ˆλ‹€.

μ„œλ²„μ—μ„œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μ‹€ν–‰ν•˜λ €λ©΄ server.js 파일이 ν¬ν•¨λœ λ””λ ‰ν† λ¦¬μ—μ„œ μœ„ λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•΄μ•Ό ν•©λ‹ˆλ‹€.

κ²°κ³Ό

[2023-03-08T08:03:00.827Z] GET /_next/static/chunks/webpack.js?ts=1678262580782
[2023-03-08T08:03:00.831Z] GET /_next/static/chunks/main.js?ts=1678262580782
[2023-03-08T08:03:00.834Z] GET /_next/static/chunks/react-refresh.js?ts=1678262580782       
[2023-03-08T08:03:00.837Z] GET /_next/static/chunks/pages/_app.js?ts=1678262580782
[2023-03-08T08:03:00.841Z] GET /_next/static/chunks/pages/index.js?ts=1678262580782
[2023-03-08T08:03:00.842Z] GET /_next/static/development/_buildManifest.js?ts=1678262580782 
[2023-03-08T08:03:00.848Z] GET /_next/static/development/_ssgManifest.js?ts=1678262580782
[2023-03-08T08:03:01.055Z] GET /_next/static/development/_devMiddlewareManifest.json
[2023-03-08T08:03:03.658Z] GET /
[2023-03-08T08:03:03.682Z] GET /_next/static/chunks/webpack.js?ts=1678262583664
[2023-03-08T08:03:03.683Z] GET /_next/static/chunks/main.js?ts=1678262583664
[2023-03-08T08:03:03.684Z] GET /_next/static/chunks/react-refresh.js?ts=1678262583664       
[2023-03-08T08:03:03.688Z] GET /_next/static/chunks/pages/_app.js?ts=1678262583664
[2023-03-08T08:03:03.689Z] GET /_next/static/chunks/pages/index.js?ts=1678262583664
[2023-03-08T08:03:03.690Z] GET /_next/static/development/_buildManifest.js?ts=1678262583664 
[2023-03-08T08:03:03.693Z] GET /_next/static/development/_ssgManifest.js?ts=1678262583664   
[2023-03-08T08:03:03.885Z] GET /_next/static/development/_devMiddlewareManifest.json
[2023-03-08T08:03:05.912Z] GET /_next/static/development/_devPagesManifest.json
[2023-03-08T08:03:05.920Z] GET /_next/static/chunks/pages/about.js
wait  - compiling /about...
@JavaPark
Welcome to Javapark blog!!