Skip to content

应用

项目拆分,导入。

// src/index.ts

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

const app = new Hono()

app.get('/', (c) => c.text('首页'))

app.route('/book', book)

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

import { Hono } from 'hono'

const book = new Hono()

// GET /book
book.get('/', (c) => c.text('图书列表'))
// GET /book/:id
book.get('/:id', (c) => {
    const id = c.req.param('id')
    return c.text('图书详情:' + id)
})
// POST /book
book.post('/', (c) => c.text('新增图书'))

export default book

编译,运行。

npm run build

npm run start

联系 math@baima.site