honojs / honox

HonoX - Hono based meta framework
https://hono.dev
MIT License
1.64k stars 43 forks source link

Middleware registered in `server.ts` doesn't work #37

Closed bruceharrison1984 closed 9 months ago

bruceharrison1984 commented 9 months ago
//app/server.ts

import { cors } from 'hono/cors';
import { createApp } from 'honox/server';
import { poweredBy } from 'hono/powered-by';
import { secureHeaders } from 'hono/secure-headers';
import { showRoutes } from 'hono/dev';
import { timing } from 'hono/timing';

const app = createApp();
app.use(
  cors(),
  secureHeaders(),
  timing(),
  poweredBy(),
  (ctx, next) => {
    console.log('middleware!');
    return next();
  }
);

showRoutes(app);

export default app;

Upon running, none of the middleware seems to be working. None of the response headers are altered per the middleware. This can also be verified because the middleware! text is never logged to the output.

mipopon commented 9 months ago

What I found working was that you create a Hono instance with middleware before and pass it to createApp function.

const base = new Hono<AppEnv>();
base.use(authMiddleware, envMiddleware);

const app = createApp({ app: base });

showRoutes(app);

export default app;
bruceharrison1984 commented 9 months ago

@mipopon Ah, ok that makes sense. Thanks!