HonoH
Hono8mo ago
Lenskha

serveStatic() doesn't serve files

I have a simple hono server with a trpc router. Unfortunately the bundled frontend isn't being served by the hono server. And API calls to the /trpc route are intercepted by the app.get('*', serveStatic({ root: '../../front/dist/index.html'})) route and also return a "404 not found".

Here is the full code
import { Hono } from 'hono'
import { trpcServer } from '@hono/trpc-server'
import { router } from './trpc';
import { cors } from 'hono/cors'
import { Session, sessionMiddleware, CookieStore } from 'hono-sessions'
import { serveStatic } from 'hono/bun'

import { imagesRouter } from './routes/images';

type sessionData = {
    userId?: string;
    isAuthenticated?: boolean;
    username?: string;
}

const app = new Hono<{
    Variables: {
        session: Session<sessionData>,
    }
}>()

const store = new CookieStore()

app.use('*', cors({
    origin: 'http://localhost:5173',
    credentials: true
}));

app.use('*', sessionMiddleware({
    store,
    sessionCookieName: 'session',
    encryptionKey: 'password_at_least_32_characters_long',
    expireAfterSeconds: 900,
}))

const appRouter = router({
    images: imagesRouter,
})

export type AppRouter = typeof appRouter;

app.use('/trpc', trpcServer({
    router: appRouter,
    createContext(_opts, c) {
        console.log('in createContext')
        return {
            session: c.get('session')
        };
    }
}));


app.get('/api/check-session', (c) => {
    const session = c.get('session')
    console.log('Session data:', {
        userId: session.get('userId'),
        isAuthenticated: session.get('isAuthenticated'),
        username: session.get('username')
    });
    return c.json({
        userId: session.get('userId'),
        isAuthenticated: session.get('isAuthenticated'),
        username: session.get('username')
    });
});

app.get('*', serveStatic({ root: '../../front/dist/index.html'}))

export default {
  port: 3000,
  fetch: app.fetch
}


Any idea as to why? i've tried with path, root...
Was this page helpful?