BA
Better Authβ€’3mo ago
Sticks

Hono Handler Not Triggering

I cannot get my hono handler to trigger right, even with app.all, or app.on. It always gives me a 404. Here is my main code:
import 'dotenv/config';
import { env } from './utils/env.js';
import { serve } from '@hono/node-server';
import { cors } from 'hono/cors';
import logger from 'logger';
import auth from './utils/auth.js';

// Init DB
import Database from './database/index.js';
import { createApp } from './utils/index.js';
import apiMainRouter from './routes/index.js';
import { log } from 'console';
const db = new Database();
const authInst = new auth(db.prisma);

async function main() {
if (!env) return;

const app = createApp();
logger.info(`πŸš€ Starting server in ${env.NODE_ENV} mode...`);

app.all('/api/auth/*', (c) => {
logger.info('Auth route hit');
return authInst.auth.handler(c.req.raw);
});

// Set up middleware
app.use((c, next) => {
c.set('db', db);
c.set('auth', authInst);
return next();
});

app.use(
cors({
origin: env.BETTER_AUTH_FRONTEND_URL,
allowHeaders: ['Content-Type', 'Authorization'],
credentials: true,
}),
);

// Pass to main route handler
app.route('/', apiMainRouter);

serve(
{
fetch: app.fetch,
port: env.PORT,
},
(info) => {
logger.info(
`πŸš€ Server is running on http://localhost:${info.port}`,
);
},
);
}

main().catch((error) => {
logger.error(':x: Fatal error during startup:');
logger.error(error);
process.exit(1);
});
import 'dotenv/config';
import { env } from './utils/env.js';
import { serve } from '@hono/node-server';
import { cors } from 'hono/cors';
import logger from 'logger';
import auth from './utils/auth.js';

// Init DB
import Database from './database/index.js';
import { createApp } from './utils/index.js';
import apiMainRouter from './routes/index.js';
import { log } from 'console';
const db = new Database();
const authInst = new auth(db.prisma);

async function main() {
if (!env) return;

const app = createApp();
logger.info(`πŸš€ Starting server in ${env.NODE_ENV} mode...`);

app.all('/api/auth/*', (c) => {
logger.info('Auth route hit');
return authInst.auth.handler(c.req.raw);
});

// Set up middleware
app.use((c, next) => {
c.set('db', db);
c.set('auth', authInst);
return next();
});

app.use(
cors({
origin: env.BETTER_AUTH_FRONTEND_URL,
allowHeaders: ['Content-Type', 'Authorization'],
credentials: true,
}),
);

// Pass to main route handler
app.route('/', apiMainRouter);

serve(
{
fetch: app.fetch,
port: env.PORT,
},
(info) => {
logger.info(
`πŸš€ Server is running on http://localhost:${info.port}`,
);
},
);
}

main().catch((error) => {
logger.error(':x: Fatal error during startup:');
logger.error(error);
process.exit(1);
});
4 Replies
Sticks
SticksOPβ€’3mo ago
@Better Auth any ideas on the above?
Better Auth
Better Authβ€’3mo ago
It sounds like you might be encountering the known issue where using toNodeHandler(auth) inside a subrouter can lead to a 404 error if the route isn’t set up to pass through the full wildcard. One workaround is to make sure you’re mounting it with a wildcard pattern (e.g., using something like "/auth/{*splat}") so that Better Auth’s routes are correctly recognized by Express [17]. Double-check your subrouter path and the basePath setting in your Better Auth configuration to ensure they match. Let me know if that helps! Sources: [17] Express: better-auth returns 404 when using toNodeHandler(auth) inside a subrouter in Express
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
GitHub
Express: better-auth returns 404 when using toNodeHandler(auth) ins...
Is this suited for github? Yes, this is suited for github To Reproduce Hi there... I'm encountering an issue when trying to use better-auth with an Express subrouter. When registering the auth ...
Sticks
SticksOPβ€’3mo ago
@Ping any ideas @Ping
Ping
Pingβ€’3mo ago
Hey, I'm not super familar with Hono APIs.. Does following the most basic form of setup using our docs work? If so it could be a user-related issue using Hono apis?

Did you find this page helpful?