Next.js Discord

Discord Forum

Support Hono.js as Next.js Custom Server

Unanswered
Australian Freshwater Crocodile posted this in #help-forum
Open in Discord
Australian Freshwater CrocodileOP
Doesn't look like the Hono.js is compatible with next.js custom server.

11 Replies

@gin do u have any errors or similar or why?
Australian Freshwater CrocodileOP
Works with express, but the but Hono Request and Response are incompatible with Next.js handle parameters.

import next from "next";
import { parse } from "url";

const app = next({ dev });
const handle = app.getRequestHandler();

const server = express()

server.all("/admin*", (req, res) => {
  const parsedUrl = parse(req.url!, true);
  handle(req, res, parsedUrl);
})
@gin thats express
Australian Freshwater CrocodileOP
I'm aware
why are u sending me this then?
@gin why are u sending me this then?
Australian Freshwater CrocodileOP
its what I have as currently working.
check this for correct usage
Australian Freshwater CrocodileOP
import { Hono } from 'hono'
import { serve } from '@hono/node-server'
import { serveStatic } from '@hono/node-server/serve-static'
import { parse } from "url";
import next from "next";
import { Bindings } from 'hono/types';

const port = parseInt(process.env.PORT || "3000", 10);
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = new Hono<{ Bindings: Bindings }>()

  server.use('/_next', serveStatic({ root: './.next' }));

  server.all("/admin*", (c) => {
    const req = c.req
    const res = c.res
    const parsedUrl = parse(req.url!, true);
    // Hono Request and Response incompatible with Next.js's Node.js handle parameters: IncomingMessage and ServerResponse
    handle(req, res, parsedUrl);
  })

  serve({ fetch: server.fetch, port }, (info) => {
    console.log(
      `> Server listening at http://localhost:${info.port} as ${dev ? "development" : process.env.NODE_ENV
      }`,
    );
  })
});