Skip to content

Node.js

// src/index.ts

import { serve } from '@hono/node-server'
import { Hono } from 'hono'

const app = new Hono()

// 文本
app.get('/', (c) => c.text('首页'))
app.get('/hi', () => new Response('天下大吉'))

// HTML
app.get('/hello', (c) => c.html('<h1>世界和平</h1>'))

// JSON
app.get('/api/hi', (c) => {
  return c.json({
    ok: true,
    message: '群龙无首',
  })
})

// 参数
app.get('/posts/:id', (c) => {
  const id = c.req.param('id')
  return c.text(`ID 为 ${id}`)
})

serve({
  fetch: app.fetch,
  port: 3000
}, (info) => {
  console.log(`服务运行在 http://localhost:${info.port}`)
})

联系 math@baima.site