Skip to content

hono.js 教程

安装

$ npm i hono
$ npm i @hono/node-server

应用开发

下面是一个最基本的服务器应用。

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

const app = new Hono();

app.get('/', (c) => {  
  return c.text('Hello!');  
});

serve(app, (info) => {  
  console.log(`Listening on http://localhost:${info.port}`);  
});

Hono 应用的默认端口是 3000。

获取 HTTP 请求

路由是/contacts/:id

const { id } = c.req.param();  

// 或  

const id = c.req.param( 'id' );

如果请求体是application/json类型,使用c.req.json()获取。

app.post('/entry', async (c) => {
  const body = await c.req.json()
  // ...
})

const { firstName, lastName } = c.req.json();  

// 或  

const firstName = c.req.json( 'firstName' );  
const lastName = c.req.json( 'lastName' );

如果请求体类型为text/plain,则需要使用c.req.text();如果请求体类型为blob,则需要使用c.req.blob()

从 URL 查询字符串中读取参数,使用c.req.query()方法。

const { limit, offset } = c.req.query();  

// 或  

const limit = c.req.query( 'limit' );  
const offset = c.req.query( 'offset' );