Skip to content
youhoc
  • Pages
    • Home
    • Modern App Guidelines
    • Node.js
      • Installing & Exploring
      • Loading Modules
      • npm - Get Command Input
      • Express.js
        • Express Web Server
        • Template Engine & MVC
        • Authentication
        • Authentication Trong REST API Với JWT
        • File Upload with Multer, Express.js
        • Server-Side Validation Với Express-Validator
      • Sequelize
        • Sequelize Transactions: Đảm Bảo Tính Toàn Vẹn Dữ Liệu
        • 7 loại Data Types phổ biến Trong Sequelize
        • Phân Trang (Pagination) Trong Express.js Với Sequelize/MySQL
      • Hướng dẫn Cơ bản về Rest API
      • Node-cron Simple to Complex Setup with PM2
      • Hono
        • icon picker
          Hono Response
        • Error Handling
    • Cloudflare
      • Minimal Cloudflare Worker + Hono + Drizzle ORM (part 1)
      • Minimal Cloudflare Worker + Hono + Drizzle ORM (part 2)
    • htmx
      • HTMx Form: Request, Response, Swap
    • Linux
      • Day 1: Linux Distributions & Navigation
      • Day 2: User Management
      • Day 3: File Permission & Ownership
      • Day 4: Package Management
      • Day 5: Services Management
    • Javascript
      • JS The Weird Part
        • Execution Context
        • Types & Operators
        • Objects & Functions
        • Error Handling & Strict Mode
        • Typescript, ES6, Tra
      • Modern JS
        • JS in the Browser
        • Data Storage JSON
        • Modern JS
        • Advanced Objects & Methods
        • Webpack & Babel
        • Async
      • jQuery
        • In-depth Analysis of jQuery
      • React-ready JS
        • Arrow Function
        • Template Literals
        • Logical AND, OR, Ternary, Nullish Operators
        • Destructuring & Rest Operator
        • Array Method
        • Immutability and Spread Operator
        • Promises, Async/Await, Callback
    • Typescript
      • TypeScript cơ bản (phần 1)
      • TypeScript cơ bản (phần 2)
      • require vs import
    • ReactJS
      • React from Andrew
        • Summary from Next
        • 1. Basics
        • 2. React Components
        • 3. Webpack
        • 4. Styling with SCSS
        • 5. React Router
        • 6. React Hook
      • Modern React From The Beginning
        • Intro to JSX
        • Vite Build Tools
        • Basic Component Creation
        • Component State
        • Props & Component Composition
        • useState with Inputs & Form Submission
        • useEffect, useRef & Local Storage
        • Async / Await and Http Request in React
        • React Router: Declarative Mode
        • ContextAPI
        • React Router: Framework Mode
          • File-routing & HTML Layouts
          • Server-side Data Query
          • Links & Navigation
          • Loaders
    • PHP
      • gruntJS
      • composer
      • MySQL
      • Thiết lập Cloudflare Turnstile chống spam trong PHP
    • Docker
      • Container Basics
      • Container Networking
      • Container Image
      • Container Volume & Persistent Data
      • Dockerfile
      • Docker Compose
      • Docker Registry
    • Payload CMS

Hono Response

Trả Response trong Hono, so sánh với Express
Nếu bạn đã quen với Express, cách trả response trong Hono sẽ không quá xa lạ — nhưng có một vài điểm khác biệt quan trọng cần nắm, đặc biệt khi làm việc với TypeScript.
Trong Express, bạn dùng res để trả response. Trong Hono, bạn dùng context c — và quan trọng là bạn phải return kết quả thay vì chỉ gọi method.

Các loại response phổ biến

JSON

Đây là loại response phổ biến nhất trong REST API.
Hono tự động set Content-Type: application/json — không cần khai báo thêm.

Text

HTML

XML, CSV, hoặc bất kỳ Content-Type nào khác

Với những định dạng Hono không có helper sẵn, dùng c.body():
Bạn cũng có thể dùng new Response() (Web Standard API) và Hono chấp nhận — nhưng dùng c.body() nhất quán hơn khi làm việc trong Hono context.

Redirect

Status Code

Trong Express, bạn hay thấy cách viết chain:
Trong Hono, status code là tham số thứ hai của helper:

Các status code phổ biến cần nhớ

Code
Ý nghĩa
Dùng khi
200
OK
GET thành công, mặc định
201
Created
POST tạo mới thành công
204
No Content
DELETE thành công, không có body
400
Bad Request
Input không hợp lệ
401
Unauthorized
Chưa đăng nhập
403
Forbidden
Đã đăng nhập nhưng không có quyền
404
Not Found
Không tìm thấy resource
422
Unprocessable Entity
Validation thất bại
500
Internal Server Error
Lỗi server không mong muốn
There are no rows in this table
Lưu ý: 204 No Content không có body — đừng gọi c.json() hay c.text() kèm theo.

Context c — req và res gộp làm một

Trong Express, bạn có reqres riêng biệt. Hono gộp cả hai vào một object duy nhất là c (Context):

Type của Context

Khi cần tách handler ra file riêng thay vì viết inline, import Context để khai báo type:

Variables — Truyền data giữa các middleware

Đây là tính năng hay bị bỏ qua, nhưng rất hữu ích khi dùng middleware.
Trong Express, bạn hay làm thế này:
Hono có cách làm tương tự nhưng type-safe hơn với c.set() / c.get(), kết hợp khai báo Variables:
Variables chỉ tồn tại trong phạm vi một request — khi response trả về, context bị huỷ và data mất theo. Đây không phải global state.
Nếu không khai báo Variables, c.get() sẽ trả về unknown và TypeScript sẽ phàn nàn.

Lưu ý cho TypeScript

Khai báo kiểu dữ liệu cho response

Hono hỗ trợ generic type cho c.json(), giúp bạn kiểm soát chính xác shape của response:

Luôn return response trong Hono

Đây là điểm khác biệt lớn nhất so với Express:
TypeScript sẽ cảnh báo nếu bạn quên return — đây là một trong những lý do nên dùng TypeScript với Hono.

Tóm tắt

Loại response
Hono
Express tương đương
JSON
c.json(data, status?)
res.status().json()
Text
c.text(str, status?)
res.send()
HTML
c.html(str, status?)
res.send()
Custom (XML…)
c.body(data, status, headers)
res.set().send()
Redirect
c.redirect(url, status?)
res.redirect()
No content
c.body(null, 204)
res.status(204).end()
There are no rows in this table
Điểm khác biệt cốt lõi so với Express: Hono yêu cầu return, dùng c thay vì res, và được thiết kế để tận dụng tối đa TypeScript từ đầu.
Bước tiếp theo: Tìm hiểu về Hono Middleware (app.use()), Validation với Zod (@hono/zod-validator), và cách tổ chức routes với Hono Router.
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.