This is a simple example of a hono API using nodejs serverless functions instead of edge for deployment on vercel. No Next.js, no extra stuff.
Deployign to edge is not always an option. For example when using drizzle to connect to a postgres database – the javascript library need the nodejs apis.
In this case you will need to deploy on "classic" serverless function instead.
I've used the default hono/vercel template when creating the project and then channged/added the following things:
Package.json
{
"type": "module",
}
This lets you use import something from "something"
instead of require("something")
.env file
NODEJS_HELPERS=0
This is needed to make sure that the serverless functions are not using the vercel's nodejs helpers for request, etc. Basically let Hono do Hono instead of Vercel's helpers.
Install @hono/node-server
npm i @hono/node-server
… so you can later to
import { Hono } from "hono";
import { handle } from "@hono/node-server/vercel";
This let's you serve the app as node-js server would but on vercel.
Remove export const config…
export const config = {
runtime: "edge",
}; // <-- remove all this
This is so that vercel doesn't serve things on the edge.
vercel dev
I wanted to serve my api from mydomain.com/
instead of mydomain.com/api/
, so I added the following rewrite rule to my vercel.json:
{
"rewrites": [
{
"source": "/(.*)",
"destination": "/api"
}
]
}
and then remove the basePath() from my index.ts so it looks like this:
const app = new Hono();
Have fun, Peter Koraca Createive Crow