Back to blog

Giai đoạn 4: Tích hợp Full-Stack với TypeScript

typescriptfullstackmonorepodeploymentproduction
Giai đoạn 4: Tích hợp Full-Stack với TypeScript

Chào mừng đến với Giai đoạn 4

Bạn đã nắm vững nền tảng TypeScript, build xong ứng dụng React type-safe và dựng được API production-ready. Giờ là lúc ghép tất cả lại với nhau.

Ở giai đoạn cuối này, bạn sẽ học cách tích hợp frontend và backend thành một ứng dụng full-stack liền mạch với shared types, kiến trúc monorepo, end-to-end type safety và chiến lược deployment production.

Đây là lúc TypeScript thật sự tỏa sáng — type chảy xuyên suốt từ database schema, qua API, tới tận các component frontend, quét sạch cả một lớp bug do tích hợp.

Thời gian: 3-4 tuần, 1-2 giờ mỗi ngày
Yêu cầu trước: Giai đoạn 1, Giai đoạn 2 và Giai đoạn 3

Bạn sẽ học được gì

Kết thúc Giai đoạn 4, bạn sẽ có thể:

✅ Tổ chức project full-stack theo kiến trúc monorepo
✅ Chia sẻ TypeScript type giữa frontend và backend
✅ Tích hợp React với Express.js API bằng TanStack Query
✅ Làm authentication end-to-end (JWT + React Context)
✅ Build tính năng real-time với WebSocket
✅ Xử lý upload file từ client lên server
✅ Setup CI/CD pipeline với GitHub Actions
✅ Deploy lên production (Docker, Railway, Vercel)
✅ Monitor và debug ứng dụng full-stack
✅ Làm API versioning và đảm bảo backwards compatibility

Kiến trúc Monorepo với NPM Workspaces

Tại sao lại là Monorepo?

Monorepo cho phép bạn quản lý nhiều package liên quan trong cùng một repository:

Lợi ích:

  • Chia sẻ code: Dùng lại type, utility và constant
  • Thay đổi atomic: Sửa frontend và backend trong cùng một lần
  • Tooling gọn hơn: Một bộ config cho linting, testing, deployment
  • Type safety: Import shared type mà không cần publish lên npm

Nhược điểm:

  • Repository nặng hơn
  • Điều phối build phức tạp hơn
  • Đòi hỏi kỷ luật trong việc quản lý dependency

Setup Monorepo

mkdir fullstack-app
cd fullstack-app
npm init -y

Tạo package.json với workspaces:

{
  "name": "fullstack-app",
  "private": true,
  "workspaces": [
    "packages/*"
  ],
  "scripts": {
    "dev": "npm run dev --workspaces --if-present",
    "build": "npm run build --workspaces --if-present",
    "test": "npm run test --workspaces --if-present"
  },
  "devDependencies": {
    "typescript": "^5.7.3",
    "prettier": "^3.4.2",
    "eslint": "^9.18.0"
  }
}

Tạo cấu trúc workspace

mkdir -p packages/shared packages/backend packages/frontend
fullstack-app/
├── package.json              # Config workspace ở root
├── packages/
│   ├── shared/               # Shared type & utility
│   │   ├── package.json
│   │   ├── tsconfig.json
│   │   └── src/
│   │       ├── types/        # TypeScript type
│   │       ├── utils/        # Utility dùng chung
│   │       └── constants/    # Constant dùng chung
│   ├── backend/              # Express.js API
│   │   ├── package.json
│   │   ├── tsconfig.json
│   │   └── src/
│   │       ├── routes/
│   │       ├── controllers/
│   │       └── services/
│   └── frontend/             # React app
│       ├── package.json
│       ├── tsconfig.json
│       └── src/
│           ├── components/
│           ├── pages/
│           └── hooks/

Xây dựng Shared Package

1. Khởi tạo Shared Package

packages/shared/package.json:

{
  "name": "@fullstack-app/shared",
  "version": "1.0.0",
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "scripts": {
    "build": "tsc",
    "dev": "tsc --watch"
  },
  "devDependencies": {
    "typescript": "^5.7.3"
  }
}

packages/shared/tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2023",
    "module": "CommonJS",
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "dist"]
}

2. Định nghĩa Shared Types

packages/shared/src/types/user.ts:

export interface User {
  id: string;
  email: string;
  name: string;
  role: 'admin' | 'user';
  createdAt: Date;
  updatedAt: Date;
}
 
export interface CreateUserInput {
  email: string;
  password: string;
  name: string;
}
 
export interface LoginInput {
  email: string;
  password: string;
}
 
export interface AuthResponse {
  user: User;
  token: string;
}
 
export type UserWithoutPassword = Omit<User, 'password'>;

packages/shared/src/types/post.ts:

export interface Post {
  id: string;
  title: string;
  content: string;
  slug: string;
  published: boolean;
  authorId: string;
  createdAt: Date;
  updatedAt: Date;
}
 
export interface CreatePostInput {
  title: string;
  content: string;
  slug: string;
  published?: boolean;
}
 
export interface UpdatePostInput {
  title?: string;
  content?: string;
  published?: boolean;
}
 
export interface PostWithAuthor extends Post {
  author: {
    id: string;
    name: string;
    email: string;
  };
}

packages/shared/src/types/api.ts:

// Wrapper chung cho API response
export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  error?: string;
  message?: string;
}
 
// Phân trang
export interface PaginatedResponse<T> {
  items: T[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}
 
export interface PaginationParams {
  page?: number;
  pageSize?: number;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}
 
// Lỗi validation
export interface ValidationError {
  field: string;
  message: string;
}

packages/shared/src/utils/validation.ts:

export const isValidEmail = (email: string): boolean => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
};
 
export const isStrongPassword = (password: string): boolean => {
  // Ít nhất 8 ký tự, 1 chữ hoa, 1 chữ thường, 1 chữ số
  return (
    password.length >= 8 &&
    /[A-Z]/.test(password) &&
    /[a-z]/.test(password) &&
    /[0-9]/.test(password)
  );
};
 
export const slugify = (text: string): string => {
  return text
    .toLowerCase()
    .trim()
    .replace(/[^\w\s-]/g, '')
    .replace(/[\s_-]+/g, '-')
    .replace(/^-+|-+$/g, '');
};

packages/shared/src/constants/index.ts:

export const API_ROUTES = {
  AUTH: {
    LOGIN: '/api/auth/login',
    REGISTER: '/api/auth/register',
    LOGOUT: '/api/auth/logout',
    REFRESH: '/api/auth/refresh',
    ME: '/api/auth/me',
  },
  POSTS: {
    LIST: '/api/posts',
    CREATE: '/api/posts',
    GET: (id: string) => `/api/posts/${id}`,
    UPDATE: (id: string) => `/api/posts/${id}`,
    DELETE: (id: string) => `/api/posts/${id}`,
  },
  USERS: {
    LIST: '/api/users',
    GET: (id: string) => `/api/users/${id}`,
    UPDATE: (id: string) => `/api/users/${id}`,
    DELETE: (id: string) => `/api/users/${id}`,
  },
} as const;
 
export const HTTP_STATUS = {
  OK: 200,
  CREATED: 201,
  NO_CONTENT: 204,
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  CONFLICT: 409,
  UNPROCESSABLE_ENTITY: 422,
  INTERNAL_SERVER_ERROR: 500,
} as const;
 
export const ERROR_MESSAGES = {
  UNAUTHORIZED: 'You must be logged in',
  FORBIDDEN: 'You do not have permission',
  NOT_FOUND: 'Resource not found',
  VALIDATION_ERROR: 'Validation failed',
  SERVER_ERROR: 'Internal server error',
} as const;

packages/shared/src/index.ts:

// Export toàn bộ type
export * from './types/user';
export * from './types/post';
export * from './types/api';
 
// Export utility
export * from './utils/validation';
 
// Export constant
export * from './constants';

3. Build Shared Package

cd packages/shared
npm run build

Lệnh này tạo thư mục dist/ chứa JavaScript đã compile cùng các file khai báo TypeScript.

Tích hợp Backend với Shared Types

1. Cài Shared Package vào Backend

packages/backend/package.json:

{
  "name": "@fullstack-app/backend",
  "version": "1.0.0",
  "scripts": {
    "dev": "tsx watch src/index.ts",
    "build": "tsc",
    "start": "node dist/index.js"
  },
  "dependencies": {
    "@fullstack-app/shared": "*",
    "express": "^5.1.0",
    "cors": "^2.8.5",
    "dotenv": "^16.4.7",
    "jsonwebtoken": "^9.0.2",
    "bcryptjs": "^2.4.3",
    "@prisma/client": "^6.5.0",
    "zod": "^3.24.1"
  },
  "devDependencies": {
    "@types/express": "^5.0.0",
    "@types/cors": "^2.8.17",
    "@types/jsonwebtoken": "^9.0.7",
    "@types/bcryptjs": "^2.4.6",
    "tsx": "^4.20.0",
    "typescript": "^5.7.3",
    "prisma": "^6.5.0"
  }
}

2. Dùng Shared Types trong Controller

packages/backend/src/controllers/authController.ts:

import { Request, Response } from 'express';
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import { prisma } from '../lib/prisma';
import {
  LoginInput,
  CreateUserInput,
  AuthResponse,
  ApiResponse,
  HTTP_STATUS,
  ERROR_MESSAGES,
} from '@fullstack-app/shared';
 
export const register = async (
  req: Request<{}, {}, CreateUserInput>,
  res: Response<ApiResponse<AuthResponse>>
) => {
  try {
    const { email, password, name } = req.body;
 
    // Kiểm tra user đã tồn tại chưa
    const existingUser = await prisma.user.findUnique({ where: { email } });
    if (existingUser) {
      return res.status(HTTP_STATUS.CONFLICT).json({
        success: false,
        error: 'User already exists',
      });
    }
 
    // Hash password
    const hashedPassword = await bcrypt.hash(password, 10);
 
    // Tạo user
    const user = await prisma.user.create({
      data: {
        email,
        password: hashedPassword,
        name,
      },
    });
 
    // Tạo token
    const token = jwt.sign(
      { userId: user.id, role: user.role },
      process.env.JWT_SECRET!,
      { expiresIn: '7d' }
    );
 
    res.status(HTTP_STATUS.CREATED).json({
      success: true,
      data: {
        user: {
          id: user.id,
          email: user.email,
          name: user.name,
          role: user.role,
          createdAt: user.createdAt,
          updatedAt: user.updatedAt,
        },
        token,
      },
    });
  } catch (error) {
    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
      success: false,
      error: ERROR_MESSAGES.SERVER_ERROR,
    });
  }
};
 
export const login = async (
  req: Request<{}, {}, LoginInput>,
  res: Response<ApiResponse<AuthResponse>>
) => {
  try {
    const { email, password } = req.body;
 
    // Tìm user
    const user = await prisma.user.findUnique({ where: { email } });
    if (!user) {
      return res.status(HTTP_STATUS.UNAUTHORIZED).json({
        success: false,
        error: 'Invalid credentials',
      });
    }
 
    // Kiểm tra password
    const validPassword = await bcrypt.compare(password, user.password);
    if (!validPassword) {
      return res.status(HTTP_STATUS.UNAUTHORIZED).json({
        success: false,
        error: 'Invalid credentials',
      });
    }
 
    // Tạo token
    const token = jwt.sign(
      { userId: user.id, role: user.role },
      process.env.JWT_SECRET!,
      { expiresIn: '7d' }
    );
 
    res.json({
      success: true,
      data: {
        user: {
          id: user.id,
          email: user.email,
          name: user.name,
          role: user.role,
          createdAt: user.createdAt,
          updatedAt: user.updatedAt,
        },
        token,
      },
    });
  } catch (error) {
    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
      success: false,
      error: ERROR_MESSAGES.SERVER_ERROR,
    });
  }
};

packages/backend/src/controllers/postController.ts:

import { Request, Response } from 'express';
import { prisma } from '../lib/prisma';
import {
  Post,
  CreatePostInput,
  UpdatePostInput,
  PostWithAuthor,
  ApiResponse,
  PaginatedResponse,
  PaginationParams,
  HTTP_STATUS,
  ERROR_MESSAGES,
} from '@fullstack-app/shared';
import { AuthRequest } from '../middleware/auth';
 
export const getPosts = async (
  req: Request<{}, {}, {}, PaginationParams>,
  res: Response<ApiResponse<PaginatedResponse<PostWithAuthor>>>
) => {
  try {
    const { page = 1, pageSize = 10, sortBy = 'createdAt', sortOrder = 'desc' } = req.query;
 
    const skip = (Number(page) - 1) * Number(pageSize);
    const take = Number(pageSize);
 
    const [posts, total] = await Promise.all([
      prisma.post.findMany({
        skip,
        take,
        orderBy: { [sortBy]: sortOrder },
        include: {
          author: {
            select: { id: true, name: true, email: true },
          },
        },
      }),
      prisma.post.count(),
    ]);
 
    res.json({
      success: true,
      data: {
        items: posts as PostWithAuthor[],
        total,
        page: Number(page),
        pageSize: Number(pageSize),
        totalPages: Math.ceil(total / Number(pageSize)),
      },
    });
  } catch (error) {
    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
      success: false,
      error: ERROR_MESSAGES.SERVER_ERROR,
    });
  }
};
 
export const createPost = async (
  req: AuthRequest<{}, {}, CreatePostInput>,
  res: Response<ApiResponse<Post>>
) => {
  try {
    const { title, content, slug, published = false } = req.body;
    const userId = req.user!.userId;
 
    const post = await prisma.post.create({
      data: {
        title,
        content,
        slug,
        published,
        authorId: userId,
      },
    });
 
    res.status(HTTP_STATUS.CREATED).json({
      success: true,
      data: post,
    });
  } catch (error) {
    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
      success: false,
      error: ERROR_MESSAGES.SERVER_ERROR,
    });
  }
};
 
export const updatePost = async (
  req: AuthRequest<{ id: string }, {}, UpdatePostInput>,
  res: Response<ApiResponse<Post>>
) => {
  try {
    const { id } = req.params;
    const userId = req.user!.userId;
    const updates = req.body;
 
    // Kiểm tra post có tồn tại không và user có phải chủ không
    const existingPost = await prisma.post.findUnique({ where: { id } });
    if (!existingPost) {
      return res.status(HTTP_STATUS.NOT_FOUND).json({
        success: false,
        error: ERROR_MESSAGES.NOT_FOUND,
      });
    }
 
    if (existingPost.authorId !== userId && req.user!.role !== 'admin') {
      return res.status(HTTP_STATUS.FORBIDDEN).json({
        success: false,
        error: ERROR_MESSAGES.FORBIDDEN,
      });
    }
 
    const post = await prisma.post.update({
      where: { id },
      data: updates,
    });
 
    res.json({
      success: true,
      data: post,
    });
  } catch (error) {
    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
      success: false,
      error: ERROR_MESSAGES.SERVER_ERROR,
    });
  }
};

Tích hợp Frontend với Shared Types

1. Cài Shared Package vào Frontend

packages/frontend/package.json:

{
  "name": "@fullstack-app/frontend",
  "version": "1.0.0",
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@fullstack-app/shared": "*",
    "react": "^19.0.0",
    "react-dom": "^19.0.0",
    "react-router-dom": "^7.1.3",
    "@tanstack/react-query": "^6.0.0",
    "axios": "^1.7.9",
    "zustand": "^5.0.3"
  },
  "devDependencies": {
    "@types/react": "^19.0.6",
    "@types/react-dom": "^19.0.2",
    "@vitejs/plugin-react": "^4.3.4",
    "typescript": "^5.7.3",
    "vite": "^6.0.7"
  }
}

2. Tạo API Client type-safe

packages/frontend/src/lib/api.ts:

import axios, { AxiosInstance } from 'axios';
import {
  ApiResponse,
  AuthResponse,
  LoginInput,
  CreateUserInput,
  User,
  Post,
  PostWithAuthor,
  CreatePostInput,
  UpdatePostInput,
  PaginatedResponse,
  PaginationParams,
  API_ROUTES,
} from '@fullstack-app/shared';
 
class ApiClient {
  private client: AxiosInstance;
 
  constructor(baseURL: string) {
    this.client = axios.create({
      baseURL,
      headers: {
        'Content-Type': 'application/json',
      },
    });
 
    // Tự gắn auth token vào mọi request
    this.client.interceptors.request.use((config) => {
      const token = localStorage.getItem('token');
      if (token) {
        config.headers.Authorization = `Bearer ${token}`;
      }
      return config;
    });
  }
 
  // Auth endpoint
  async login(data: LoginInput): Promise<AuthResponse> {
    const response = await this.client.post<ApiResponse<AuthResponse>>(
      API_ROUTES.AUTH.LOGIN,
      data
    );
    return response.data.data!;
  }
 
  async register(data: CreateUserInput): Promise<AuthResponse> {
    const response = await this.client.post<ApiResponse<AuthResponse>>(
      API_ROUTES.AUTH.REGISTER,
      data
    );
    return response.data.data!;
  }
 
  async getMe(): Promise<User> {
    const response = await this.client.get<ApiResponse<User>>(
      API_ROUTES.AUTH.ME
    );
    return response.data.data!;
  }
 
  async logout(): Promise<void> {
    await this.client.post(API_ROUTES.AUTH.LOGOUT);
  }
 
  // Post endpoint
  async getPosts(params?: PaginationParams): Promise<PaginatedResponse<PostWithAuthor>> {
    const response = await this.client.get<ApiResponse<PaginatedResponse<PostWithAuthor>>>(
      API_ROUTES.POSTS.LIST,
      { params }
    );
    return response.data.data!;
  }
 
  async getPost(id: string): Promise<PostWithAuthor> {
    const response = await this.client.get<ApiResponse<PostWithAuthor>>(
      API_ROUTES.POSTS.GET(id)
    );
    return response.data.data!;
  }
 
  async createPost(data: CreatePostInput): Promise<Post> {
    const response = await this.client.post<ApiResponse<Post>>(
      API_ROUTES.POSTS.CREATE,
      data
    );
    return response.data.data!;
  }
 
  async updatePost(id: string, data: UpdatePostInput): Promise<Post> {
    const response = await this.client.put<ApiResponse<Post>>(
      API_ROUTES.POSTS.UPDATE(id),
      data
    );
    return response.data.data!;
  }
 
  async deletePost(id: string): Promise<void> {
    await this.client.delete(API_ROUTES.POSTS.DELETE(id));
  }
}
 
export const api = new ApiClient(import.meta.env.VITE_API_URL || 'http://localhost:4000');

3. Tạo Auth Store với Zustand

packages/frontend/src/stores/authStore.ts:

import { create } from 'zustand';
import { User } from '@fullstack-app/shared';
import { api } from '../lib/api';
 
interface AuthState {
  user: User | null;
  token: string | null;
  isAuthenticated: boolean;
  isLoading: boolean;
  login: (email: string, password: string) => Promise<void>;
  register: (email: string, password: string, name: string) => Promise<void>;
  logout: () => void;
  fetchUser: () => Promise<void>;
}
 
export const useAuthStore = create<AuthState>((set) => ({
  user: null,
  token: localStorage.getItem('token'),
  isAuthenticated: !!localStorage.getItem('token'),
  isLoading: false,
 
  login: async (email, password) => {
    set({ isLoading: true });
    try {
      const { user, token } = await api.login({ email, password });
      localStorage.setItem('token', token);
      set({ user, token, isAuthenticated: true, isLoading: false });
    } catch (error) {
      set({ isLoading: false });
      throw error;
    }
  },
 
  register: async (email, password, name) => {
    set({ isLoading: true });
    try {
      const { user, token } = await api.register({ email, password, name });
      localStorage.setItem('token', token);
      set({ user, token, isAuthenticated: true, isLoading: false });
    } catch (error) {
      set({ isLoading: false });
      throw error;
    }
  },
 
  logout: () => {
    localStorage.removeItem('token');
    set({ user: null, token: null, isAuthenticated: false });
    api.logout().catch(() => {});
  },
 
  fetchUser: async () => {
    try {
      const user = await api.getMe();
      set({ user });
    } catch (error) {
      // Token không hợp lệ, xóa trạng thái auth
      localStorage.removeItem('token');
      set({ user: null, token: null, isAuthenticated: false });
    }
  },
}));

4. Tạo React Component dùng Shared Types

packages/frontend/src/components/LoginForm.tsx:

import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAuthStore } from '../stores/authStore';
import { isValidEmail } from '@fullstack-app/shared';
 
export function LoginForm() {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [error, setError] = useState('');
  const { login, isLoading } = useAuthStore();
  const navigate = useNavigate();
 
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setError('');
 
    if (!isValidEmail(email)) {
      setError('Invalid email address');
      return;
    }
 
    try {
      await login(email, password);
      navigate('/dashboard');
    } catch (err) {
      setError('Invalid credentials');
    }
  };
 
  return (
    <form onSubmit={handleSubmit} className="space-y-4">
      <div>
        <label htmlFor="email" className="block text-sm font-medium">
          Email
        </label>
        <input
          id="email"
          type="email"
          value={email}
          onChange={(e) => setEmail(e.target.value)}
          className="mt-1 block w-full rounded border p-2"
          required
        />
      </div>
 
      <div>
        <label htmlFor="password" className="block text-sm font-medium">
          Password
        </label>
        <input
          id="password"
          type="password"
          value={password}
          onChange={(e) => setPassword(e.target.value)}
          className="mt-1 block w-full rounded border p-2"
          required
        />
      </div>
 
      {error && <p className="text-red-500 text-sm">{error}</p>}
 
      <button
        type="submit"
        disabled={isLoading}
        className="w-full bg-blue-500 text-white py-2 rounded hover:bg-blue-600 disabled:opacity-50"
      >
        {isLoading ? 'Logging in...' : 'Login'}
      </button>
    </form>
  );
}

packages/frontend/src/components/PostList.tsx:

import { useQuery } from '@tanstack/react-query';
import { api } from '../lib/api';
import { PostWithAuthor } from '@fullstack-app/shared';
 
export function PostList() {
  const { data, isLoading, error } = useQuery({
    queryKey: ['posts'],
    queryFn: () => api.getPosts({ page: 1, pageSize: 10 }),
  });
 
  if (isLoading) return <div>Loading posts...</div>;
  if (error) return <div>Error loading posts</div>;
  if (!data) return null;
 
  return (
    <div className="space-y-4">
      <h2 className="text-2xl font-bold">Posts ({data.total})</h2>
      
      <div className="grid gap-4">
        {data.items.map((post: PostWithAuthor) => (
          <article key={post.id} className="border rounded p-4">
            <h3 className="text-xl font-semibold">{post.title}</h3>
            <p className="text-gray-600 mt-2">{post.content.slice(0, 150)}...</p>
            <div className="mt-2 text-sm text-gray-500">
              By {post.author.name} • {new Date(post.createdAt).toLocaleDateString()}
            </div>
          </article>
        ))}
      </div>
 
      <div className="flex justify-between items-center">
        <p className="text-sm text-gray-600">
          Page {data.page} of {data.totalPages}
        </p>
      </div>
    </div>
  );
}

Luồng Authentication End-to-End

Backend: JWT Middleware

packages/backend/src/middleware/auth.ts:

import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import { HTTP_STATUS, ERROR_MESSAGES } from '@fullstack-app/shared';
 
export interface AuthRequest<P = any, ResBody = any, ReqBody = any> 
  extends Request<P, ResBody, ReqBody> {
  user?: {
    userId: string;
    role: 'admin' | 'user';
  };
}
 
export const authenticate = (
  req: AuthRequest,
  res: Response,
  next: NextFunction
) => {
  try {
    const token = req.headers.authorization?.replace('Bearer ', '');
    
    if (!token) {
      return res.status(HTTP_STATUS.UNAUTHORIZED).json({
        success: false,
        error: ERROR_MESSAGES.UNAUTHORIZED,
      });
    }
 
    const decoded = jwt.verify(token, process.env.JWT_SECRET!) as {
      userId: string;
      role: 'admin' | 'user';
    };
 
    req.user = decoded;
    next();
  } catch (error) {
    res.status(HTTP_STATUS.UNAUTHORIZED).json({
      success: false,
      error: 'Invalid token',
    });
  }
};
 
export const requireAdmin = (
  req: AuthRequest,
  res: Response,
  next: NextFunction
) => {
  if (req.user?.role !== 'admin') {
    return res.status(HTTP_STATUS.FORBIDDEN).json({
      success: false,
      error: ERROR_MESSAGES.FORBIDDEN,
    });
  }
  next();
};

Frontend: Protected Route

packages/frontend/src/components/ProtectedRoute.tsx:

import { Navigate } from 'react-router-dom';
import { useAuthStore } from '../stores/authStore';
 
interface ProtectedRouteProps {
  children: React.ReactNode;
}
 
export function ProtectedRoute({ children }: ProtectedRouteProps) {
  const { isAuthenticated } = useAuthStore();
 
  if (!isAuthenticated) {
    return <Navigate to="/login" replace />;
  }
 
  return <>{children}</>;
}

packages/frontend/src/App.tsx:

import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { LoginForm } from './components/LoginForm';
import { PostList } from './components/PostList';
import { ProtectedRoute } from './components/ProtectedRoute';
 
const queryClient = new QueryClient();
 
export function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <BrowserRouter>
        <Routes>
          <Route path="/login" element={<LoginForm />} />
          <Route
            path="/dashboard"
            element={
              <ProtectedRoute>
                <PostList />
              </ProtectedRoute>
            }
          />
        </Routes>
      </BrowserRouter>
    </QueryClientProvider>
  );
}

Tính năng Real-Time với WebSocket

Backend: Setup Socket.io

cd packages/backend
npm install socket.io
npm install --save-dev @types/socket.io

packages/backend/src/socket/index.ts:

import { Server } from 'socket.io';
import { Server as HttpServer } from 'http';
import jwt from 'jsonwebtoken';
 
export function setupWebSocket(httpServer: HttpServer) {
  const io = new Server(httpServer, {
    cors: {
      origin: process.env.FRONTEND_URL || 'http://localhost:5173',
      credentials: true,
    },
  });
 
  // Middleware xác thực
  io.use((socket, next) => {
    const token = socket.handshake.auth.token;
    
    if (!token) {
      return next(new Error('Authentication error'));
    }
 
    try {
      const decoded = jwt.verify(token, process.env.JWT_SECRET!);
      socket.data.user = decoded;
      next();
    } catch (err) {
      next(new Error('Invalid token'));
    }
  });
 
  io.on('connection', (socket) => {
    console.log('User connected:', socket.data.user.userId);
 
    socket.on('join-room', (roomId: string) => {
      socket.join(roomId);
      console.log(`User ${socket.data.user.userId} joined room ${roomId}`);
    });
 
    socket.on('send-message', (data: { roomId: string; message: string }) => {
      io.to(data.roomId).emit('new-message', {
        userId: socket.data.user.userId,
        message: data.message,
        timestamp: new Date(),
      });
    });
 
    socket.on('disconnect', () => {
      console.log('User disconnected:', socket.data.user.userId);
    });
  });
 
  return io;
}

packages/backend/src/index.ts:

import express from 'express';
import http from 'http';
import cors from 'cors';
import { setupWebSocket } from './socket';
 
const app = express();
const httpServer = http.createServer(app);
 
app.use(cors());
app.use(express.json());
 
// Setup WebSocket
setupWebSocket(httpServer);
 
// Routes...
 
const PORT = process.env.PORT || 4000;
httpServer.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

Frontend: Socket.io Client

cd packages/frontend
npm install socket.io-client

packages/frontend/src/hooks/useWebSocket.ts:

import { useEffect, useState } from 'react';
import { io, Socket } from 'socket.io-client';
import { useAuthStore } from '../stores/authStore';
 
export function useWebSocket() {
  const [socket, setSocket] = useState<Socket | null>(null);
  const { token } = useAuthStore();
 
  useEffect(() => {
    if (!token) return;
 
    const newSocket = io(import.meta.env.VITE_API_URL || 'http://localhost:4000', {
      auth: { token },
    });
 
    newSocket.on('connect', () => {
      console.log('WebSocket connected');
    });
 
    newSocket.on('disconnect', () => {
      console.log('WebSocket disconnected');
    });
 
    setSocket(newSocket);
 
    return () => {
      newSocket.close();
    };
  }, [token]);
 
  return socket;
}

Triển khai Upload File

Backend: Setup Multer

cd packages/backend
npm install multer
npm install --save-dev @types/multer

packages/backend/src/middleware/upload.ts:

import multer from 'multer';
import path from 'path';
 
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'uploads/');
  },
  filename: (req, file, cb) => {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
    cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
  },
});
 
const fileFilter = (req: any, file: Express.Multer.File, cb: multer.FileFilterCallback) => {
  const allowedTypes = /jpeg|jpg|png|gif|pdf/;
  const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
  const mimetype = allowedTypes.test(file.mimetype);
 
  if (extname && mimetype) {
    cb(null, true);
  } else {
    cb(new Error('Invalid file type'));
  }
};
 
export const upload = multer({
  storage,
  fileFilter,
  limits: {
    fileSize: 5 * 1024 * 1024, // Giới hạn 5MB
  },
});

packages/backend/src/routes/upload.ts:

import express from 'express';
import { upload } from '../middleware/upload';
import { authenticate } from '../middleware/auth';
 
const router = express.Router();
 
router.post('/upload', authenticate, upload.single('file'), (req, res) => {
  if (!req.file) {
    return res.status(400).json({
      success: false,
      error: 'No file uploaded',
    });
  }
 
  res.json({
    success: true,
    data: {
      filename: req.file.filename,
      path: `/uploads/${req.file.filename}`,
      size: req.file.size,
    },
  });
});
 
export default router;

Frontend: Component upload file

packages/frontend/src/components/FileUpload.tsx:

import { useState } from 'react';
import axios from 'axios';
 
export function FileUpload() {
  const [file, setFile] = useState<File | null>(null);
  const [uploading, setUploading] = useState(false);
  const [uploadedUrl, setUploadedUrl] = useState('');
 
  const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.files && e.target.files[0]) {
      setFile(e.target.files[0]);
    }
  };
 
  const handleUpload = async () => {
    if (!file) return;
 
    const formData = new FormData();
    formData.append('file', file);
 
    setUploading(true);
    try {
      const token = localStorage.getItem('token');
      const response = await axios.post(
        `${import.meta.env.VITE_API_URL}/api/upload`,
        formData,
        {
          headers: {
            'Content-Type': 'multipart/form-data',
            Authorization: `Bearer ${token}`,
          },
        }
      );
 
      setUploadedUrl(response.data.data.path);
      alert('File uploaded successfully!');
    } catch (error) {
      alert('Upload failed');
    } finally {
      setUploading(false);
    }
  };
 
  return (
    <div className="space-y-4">
      <input type="file" onChange={handleFileChange} />
      
      <button
        onClick={handleUpload}
        disabled={!file || uploading}
        className="px-4 py-2 bg-blue-500 text-white rounded disabled:opacity-50"
      >
        {uploading ? 'Uploading...' : 'Upload File'}
      </button>
 
      {uploadedUrl && (
        <p className="text-sm text-green-600">
          File uploaded: {uploadedUrl}
        </p>
      )}
    </div>
  );
}

Chiến lược Deployment

Deploy bằng Docker

Dockerfile ở root (multi-stage):

# Stage 1: Build shared package
FROM node:22-alpine AS shared-builder
WORKDIR /app
COPY package*.json ./
COPY packages/shared/package*.json ./packages/shared/
RUN npm install --workspace=@fullstack-app/shared
COPY packages/shared ./packages/shared
RUN npm run build --workspace=@fullstack-app/shared
 
# Stage 2: Build backend
FROM node:22-alpine AS backend-builder
WORKDIR /app
COPY package*.json ./
COPY packages/backend/package*.json ./packages/backend/
COPY --from=shared-builder /app/packages/shared ./packages/shared
RUN npm install --workspace=@fullstack-app/backend
COPY packages/backend ./packages/backend
RUN npm run build --workspace=@fullstack-app/backend
 
# Stage 3: Build frontend
FROM node:22-alpine AS frontend-builder
WORKDIR /app
COPY package*.json ./
COPY packages/frontend/package*.json ./packages/frontend/
COPY --from=shared-builder /app/packages/shared ./packages/shared
RUN npm install --workspace=@fullstack-app/frontend
COPY packages/frontend ./packages/frontend
RUN npm run build --workspace=@fullstack-app/frontend
 
# Stage 4: Production backend
FROM node:22-alpine AS backend-production
WORKDIR /app
COPY --from=backend-builder /app/packages/backend/dist ./dist
COPY --from=backend-builder /app/packages/backend/package*.json ./
COPY --from=shared-builder /app/packages/shared/dist ./node_modules/@fullstack-app/shared/dist
RUN npm install --production
EXPOSE 4000
CMD ["node", "dist/index.js"]
 
# Stage 5: Production frontend (Nginx)
FROM nginx:alpine AS frontend-production
COPY --from=frontend-builder /app/packages/frontend/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

docker-compose.yml:

version: '3.8'
 
services:
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: fullstack
      POSTGRES_PASSWORD: password
      POSTGRES_DB: fullstack_db
    volumes:
      - postgres-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
 
  backend:
    build:
      context: .
      target: backend-production
    environment:
      DATABASE_URL: postgresql://fullstack:password@postgres:5432/fullstack_db
      JWT_SECRET: your-secret-key
      NODE_ENV: production
    depends_on:
      - postgres
    ports:
      - "4000:4000"
 
  frontend:
    build:
      context: .
      target: frontend-production
    environment:
      VITE_API_URL: http://localhost:4000
    ports:
      - "80:80"
    depends_on:
      - backend
 
volumes:
  postgres-data:

Deploy với Railway

Railway hỗ trợ monorepo ngay từ đầu, không cần config thêm gì nhiều:

  1. Kết nối GitHub repository
  2. Tạo service riêng cho backend và frontend
  3. Cấu hình build command:
    • Backend: npm install && npm run build --workspace=@fullstack-app/backend
    • Frontend: npm install && npm run build --workspace=@fullstack-app/frontend
  4. Set biến môi trường cho từng service
  5. Railway tự nhận diện port và lo luôn phần DNS

Deploy Frontend lên Vercel

vercel.json:

{
  "buildCommand": "npm install && npm run build --workspace=@fullstack-app/frontend",
  "outputDirectory": "packages/frontend/dist",
  "framework": "vite"
}

CI/CD với GitHub Actions

.github/workflows/ci.yml:

name: CI/CD Pipeline
 
on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]
 
jobs:
  test-shared:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '22'
      - run: npm ci
      - run: npm run build --workspace=@fullstack-app/shared
      - run: npm run test --workspace=@fullstack-app/shared
 
  test-backend:
    runs-on: ubuntu-latest
    needs: test-shared
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_PASSWORD: password
          POSTGRES_DB: test_db
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '22'
      - run: npm ci
      - run: npm run build --workspace=@fullstack-app/shared
      - run: npm run build --workspace=@fullstack-app/backend
      - run: npm run test --workspace=@fullstack-app/backend
        env:
          DATABASE_URL: postgresql://postgres:password@localhost:5432/test_db
 
  test-frontend:
    runs-on: ubuntu-latest
    needs: test-shared
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '22'
      - run: npm ci
      - run: npm run build --workspace=@fullstack-app/shared
      - run: npm run build --workspace=@fullstack-app/frontend
      - run: npm run test --workspace=@fullstack-app/frontend
 
  deploy:
    runs-on: ubuntu-latest
    needs: [test-backend, test-frontend]
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - name: Deploy to Railway
        run: |
          curl -X POST ${{ secrets.RAILWAY_WEBHOOK_URL }}

Monitoring và Debugging

Backend: Structured Logging

npm install winston --workspace=@fullstack-app/backend

packages/backend/src/lib/logger.ts:

import winston from 'winston';
 
export const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.errors({ stack: true }),
    winston.format.json()
  ),
  transports: [
    new winston.transports.Console({
      format: winston.format.combine(
        winston.format.colorize(),
        winston.format.simple()
      ),
    }),
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    new winston.transports.File({ filename: 'combined.log' }),
  ],
});

Frontend: Error Boundary

packages/frontend/src/components/ErrorBoundary.tsx:

import { Component, ErrorInfo, ReactNode } from 'react';
 
interface Props {
  children: ReactNode;
  fallback?: ReactNode;
}
 
interface State {
  hasError: boolean;
  error?: Error;
}
 
export class ErrorBoundary extends Component<Props, State> {
  constructor(props: Props) {
    super(props);
    this.state = { hasError: false };
  }
 
  static getDerivedStateFromError(error: Error): State {
    return { hasError: true, error };
  }
 
  componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error('Error caught by boundary:', error, errorInfo);
    // Gửi sang dịch vụ error tracking (Sentry, v.v.)
  }
 
  render() {
    if (this.state.hasError) {
      return this.props.fallback || (
        <div className="p-4 bg-red-50 border border-red-200 rounded">
          <h2 className="text-lg font-semibold text-red-800">
            Something went wrong
          </h2>
          <p className="text-sm text-red-600 mt-2">
            {this.state.error?.message}
          </p>
        </div>
      );
    }
 
    return this.props.children;
  }
}

Tổng hợp Best Practices

Cấu trúc project

✅ Dùng monorepo để chia sẻ type cho cả full-stack
✅ Giữ shared package không có dependency (chỉ chứa type)
✅ Version shared package theo semantic versioning
✅ Ghi chú lại các breaking change trong shared type

Type Safety

✅ Dùng cấu hình TypeScript strict
✅ Định nghĩa API contract trong shared package
✅ Type đầy đủ cho API response và các trạng thái lỗi
✅ Dùng discriminated union cho API response

Tích hợp API

✅ Tạo một API client tập trung
✅ Dùng React Query để quản lý server state
✅ Làm optimistic update
✅ Xử lý trạng thái loading và error cho mượt

Authentication

✅ Lưu JWT trong httpOnly cookie (tốt nhất) hoặc localStorage
✅ Có cơ chế refresh token
✅ Xóa auth state khi nhận response 401
✅ Bảo vệ route ở cả frontend lẫn backend

Testing

✅ Viết integration test cho các API endpoint
✅ Test component với API response được mock
✅ Test shared utility độc lập
✅ Tự động hóa test trong CI pipeline

Deployment

✅ Dùng biến môi trường cho configuration
✅ Có health check endpoint
✅ Setup logging và monitoring
✅ Dùng Docker để deploy nhất quán
✅ Áp dụng blue-green hoặc rolling deployment

Những cái bẫy thường gặp

❌ Circular dependency giữa các package
✅ Giữ shared package "ngây thơ", chỉ chứa type

❌ Quên rebuild shared package
✅ Chạy npm run dev ở watch mode hoặc dùng pre-build script

❌ Gắn frontend trực tiếp vào database schema
✅ Tạo type riêng cho API và transform ở phía server

❌ Không xử lý API error
✅ Dùng try-catch và hiển thị message thân thiện cho user

❌ Để secret trong code frontend
✅ Dùng biến môi trường, tuyệt đối không commit file .env

❌ Không version API endpoint
✅ Dùng prefix /api/v1/ để đảm bảo backwards compatibility

Bước tiếp theo

Chúc mừng bạn! Bạn đã hoàn thành Lộ trình TypeScript Full-Stack. Giờ bạn đã biết cách:

  • Build frontend React type-safe
  • Tạo backend Node.js vững chắc
  • Chia sẻ type xuyên suốt cả stack
  • Tích hợp frontend và backend liền mạch
  • Deploy ứng dụng full-stack lên production

Tiếp tục học

Các chủ đề nâng cao nên khám phá:

  • Kiến trúc microservice với TypeScript
  • GraphQL với TypeScript code generation
  • Server-Side Rendering (SSR) với Next.js
  • Tính năng cộng tác real-time
  • Chiến lược deployment nâng cao (Kubernetes, AWS)
  • Tối ưu performance và chiến lược caching

Các lộ trình khác:

Tài nguyên


Deep Dive

Sẵn sàng thành thạo các mảng cụ thể? Xem các bài deep dive:

📘 Deep Dive: Kiểu TypeScript nâng cao → — Conditional type, mapped type, branded type
📘 Deep Dive: React với TypeScript → — Component pattern, hooks typing, performance
📘 Deep Dive: Phát triển API Node.js → — Pattern API production
📘 Deep Dive: Database & ORM → — Prisma, Drizzle, testing
📘 Deep Dive: Testing & DevOps → — Vitest, Playwright, CI/CD


Trước đó: Giai đoạn 3: Backend Development
Series: Lộ trình TypeScript Full-Stack
Tiếp theo: Deep Dive: Kiểu TypeScript nâng cao

Happy coding! 🚀

📬 Đăng ký nhận bản tin

Nhận những bài viết mới nhất qua email mỗi tuần. Không spam, hủy đăng ký bất cứ lúc nào.

Chúng tôi tôn trọng quyền riêng tư của bạn. Hủy đăng ký bất cứ lúc nào.

💬 Comments

Đăng nhập để bình luận

Chúng tôi sẽ không đăng bất cứ điều gì mà không có sự cho phép của bạn.