node-server icon indicating copy to clipboard operation
node-server copied to clipboard

[WIP] feat: introduce `hono-node-server` script to serve Hono app

Open usualoma opened this issue 2 months ago • 5 comments

Starting with Node.js v24, experimental-strip-types is enabled by default. This allows us to run an app built with Hono in .ts format using very simple code and without external dependencies, simply by adding this PR.

While there are limitations, I find it convenient that you can run it in Node.js simply by changing the command, using exactly the same code as Deno or Bun.

$ cat app.ts
import { Hono } from 'hono'

const app = new Hono()
app.get('/', async (c) => {
  return c.json({ message: 'Hello World' })
})

export default app

$ npx @hono/node-server app.ts
Listening on http://localhost:3000

usualoma avatar Oct 04 '25 01:10 usualoma

Hi @yusukebe

It's not exactly “super convenient,” but the ability to run on multiple runtimes with completely identical code feels Hono-like. What do you think?

usualoma avatar Oct 04 '25 01:10 usualoma

@usualoma

Ah, interesting! Why have I never come up with this! I'll check the details later.

yusukebe avatar Oct 05 '25 08:10 yusukebe

Hey @usualoma !

I think we don't have to hurry to decide whether to accept this PR or not. We have to consider well. I'll share my thoughts about this feature:

How about making this not depend on Hono

This script can be used for the app that uses the fetch interface, not Hono. Like this app:

export default {
  fetch: () => new Response('Hi')
}

So, it may be good to design support for all fetch interface apps.

I want to run JSX with this!

If we use this script, the app is usually run on Node.js. Now, it's wonderful that Node.js can run TypeScript! However, if the app uses JSX, it can't do so. I love the JSX feature of Hono. So it's better to work with JSX. I think we can do it with a builder like esbuild, though I don't know too much about the software for building. I have to investigate it.

But, to keep it simple, it will be better only to rely on Node.js type stripping.

Is it best to create the script in this @hono/node-server?

There are several options for distributing this script, not just including it in this @hono/node-server package. If we make it so it does not depend on Hono, it will be more effective. For example, we can create node-server-for-fetch-app (this is done intentionally for clarity). If so, it can cover more general use-cases. And we can name the script. For example, how about s as super super short?

$ s src/index.ts

(I like the naming serve, but there is already theserve package: https://www.npmjs.com/package/serve)

WinterTC started to consider the HTTP server API

This not only matters in Node.js matters, but, interestingly, WinterTC started to discuss the HTTP server API: https://github.com/WinterTC55/admin/issues/137

If this is accepted, Node.js can follow the spec, and the interface will be implemented; therefore, we won't need to use this script. But it's a better idea to use this until it is enabled.


Anyway, this is an exciting idea.

yusukebe avatar Oct 06 '25 23:10 yusukebe

This is a similar idea: https://github.com/h3js/srvx

yusukebe avatar Oct 08 '25 08:10 yusukebe

Hi @yusukebe, thanks for the comment.

Indeed, it would be great if JSX worked too.

It seems https://github.com/h3js/srvx uses https://github.com/unjs/jiti.

https://github.com/h3js/srvx/blob/main/src/cli.ts#L265

usualoma avatar Oct 08 '25 09:10 usualoma