Better AuthBA
Better Authβ€’5mo 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);
});
Was this page helpful?