Skip to content

pkorac/serverlesshono

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NodeJS Severless Hono on Vercel

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.

Why not on Edge?

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.

Deployment Tips and Tricks

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.

Running locally

vercel dev

Other usefull stuff

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

About

Hono deployed on Vercel as serverless ndoejs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published