Skip to content

Vercel

Vercel 是一个为前端开发者打造的平台,为创新者提供灵感迸发时所需的速度和可靠性。本节介绍在 Vercel 上运行的 Next.js。

Next.js 是一个灵活的 React 框架,为创建快速 Web 应用程序提供了构建模块。

在 Next.js 中,Edge Functions(边缘函数)允许你在 Vercel 等边缘运行时上创建动态 API。 使用 Hono,你可以使用与其他运行时相同的语法编写 API,并使用许多中间件。

1. 环境搭建

我们提供了 Next.js 的启动模板。 使用 "create-hono" 命令启动你的项目。 在本例中选择 nextjs 模板。

sh
npm create hono@latest my-app
sh
yarn create hono my-app
sh
pnpm create hono my-app
sh
bun create hono@latest my-app
sh
deno init --npm hono my-app

进入 my-app 目录并安装依赖。

sh
cd my-app
npm i
sh
cd my-app
yarn
sh
cd my-app
pnpm i
sh
cd my-app
bun i

2. Hello World

如果你使用 App Router,编辑 app/api/[[...route]]/route.ts。更多选项请参考 支持的 HTTP 方法 部分。

ts
import { Hono } from 'hono'
import { handle } from 'hono/vercel'

export const runtime = 'edge'

const app = new Hono().basePath('/api')

app.get('/hello', (c) => {
  return c.json({
    message: 'Hello Next.js!',
  })
})

export const GET = handle(app)
export const POST = handle(app)

如果你使用 Pages Router,编辑 pages/api/[[...route]].ts

ts
import { Hono } from 'hono'
import { handle } from 'hono/vercel'
import type { PageConfig } from 'next'

export const config: PageConfig = {
  runtime: 'edge',
}

const app = new Hono().basePath('/api')

app.get('/hello', (c) => {
  return c.json({
    message: 'Hello Next.js!',
  })
})

export default handle(app)

3. 运行

在本地运行开发服务器。然后在浏览器中访问 http://localhost:3000

sh
npm run dev
sh
yarn dev
sh
pnpm dev
sh
bun run dev

现在,/api/hello 只返回 JSON,但如果你构建 React UI,你可以使用 Hono 创建一个全栈应用程序。

4. 部署

如果你有 Vercel 账号,你可以通过链接 Git 仓库进行部署。

Node.js

你也可以在 Node.js 运行时的 Next.js 上运行 Hono。

App Router

对于 App Router,你只需要在路由处理程序中将运行时设置为 nodejs

ts
import { Hono } from 'hono'
import { handle } from 'hono/vercel'

export const runtime = 'nodejs'

const app = new Hono().basePath('/api')

app.get('/hello', (c) => {
  return c.json({
    message: 'Hello from Hono!',
  })
})

export const GET = handle(app)
export const POST = handle(app)

Pages Router

对于 Pages Router,你首先需要安装 Node.js 适配器:

sh
npm i @hono/node-server
sh
yarn add @hono/node-server
sh
pnpm add @hono/node-server
sh
bun add @hono/node-server

然后,你可以使用从 @hono/node-server/vercel 导入的 handle 函数:

ts
import { Hono } from 'hono'
import { handle } from '@hono/node-server/vercel'
import type { PageConfig } from 'next'

export const config: PageConfig = {
  api: {
    bodyParser: false,
  },
}

const app = new Hono().basePath('/api')

app.get('/hello', (c) => {
  return c.json({
    message: 'Hello from Hono!',
  })
})

export default handle(app)

为了让这在 Pages Router 中正常工作,需要通过在项目仪表板或 .env 文件中设置环境变量来禁用 Vercel Node.js 辅助工具:

text
NODEJS_HELPERS=0

Released under the MIT License.