@@ -65,21 +65,65 @@ app.use('*', async (c, next) => {
6565// Global error handler middleware
6666app . use ( '*' , errorHandler ) ;
6767
68- // Serve static files from the public directory
69- app . use ( '*' , serveStatic ( {
70- root : './public' ,
71- rewriteRequestPath : ( path ) => {
72- console . log ( `Static file request for: ${ path } ` ) ;
73- return path ;
68+ // Register all API routes
69+ registerRoutes ( app ) ;
70+
71+ // Health check endpoint
72+ app . get ( '/' , async ( c ) => {
73+ // If the request accepts HTML, serve the index.html file directly
74+ if ( c . req . header ( 'accept' ) ?. includes ( 'text/html' ) ) {
75+ try {
76+ const indexPath = path . resolve ( __dirname , '../public/index.html' ) ;
77+ const content = fs . readFileSync ( indexPath , 'utf-8' ) ;
78+ return c . html ( content ) ;
79+ } catch ( error ) {
80+ console . error ( `Error serving index.html: ${ error . message } ` ) ;
81+ return c . text ( 'Internal Server Error' , 500 ) ;
82+ }
7483 }
75- } ) ) ;
84+
85+ // Otherwise, return a JSON response
86+ return c . json ( {
87+ status : 'ok' ,
88+ message : 'Document generation service is running' ,
89+ version : process . env . npm_package_version || '1.0.0'
90+ } ) ;
91+ } ) ;
92+
93+ // Serve static files from the public directory, but skip API routes
94+ app . use ( '*' , async ( c , next ) => {
95+ const reqPath = c . req . path ;
96+
97+ // Skip API routes
98+ if ( reqPath . startsWith ( '/api/' ) ) {
99+ return next ( ) ;
100+ }
101+
102+ // Use the static file middleware for non-API routes
103+ return serveStatic ( {
104+ root : './public' ,
105+ rewriteRequestPath : ( path ) => {
106+ console . log ( `Static file request for: ${ path } ` ) ;
107+ return path ;
108+ }
109+ } ) ( c , next ) ;
110+ } ) ;
76111
77112// SPA fallback for routes that don't match any static files
78113app . get ( '*' , async ( c ) => {
79114 const reqPath = c . req . path ;
80115
81- // Skip API routes and files with extensions
82- if ( reqPath . startsWith ( '/api/' ) || reqPath . includes ( '.' ) ) {
116+ // Add detailed logging for API routes
117+ if ( reqPath . startsWith ( '/api/' ) ) {
118+ console . log ( `API route not found: ${ reqPath } ` ) ;
119+ return c . json ( {
120+ error : 'API endpoint not found' ,
121+ path : reqPath
122+ } , 404 ) ;
123+ }
124+
125+ // Skip files with extensions
126+ if ( reqPath . includes ( '.' ) ) {
83127 return c . notFound ( ) ;
84128 }
85129
@@ -96,24 +140,6 @@ app.get('*', async (c) => {
96140 }
97141} ) ;
98142
99- // Health check endpoint
100- app . get ( '/' , ( c ) => {
101- // If the request accepts HTML, let the static middleware handle it
102- if ( c . req . header ( 'accept' ) ?. includes ( 'text/html' ) ) {
103- return c . next ( ) ;
104- }
105-
106- // Otherwise, return a JSON response
107- return c . json ( {
108- status : 'ok' ,
109- message : 'Document generation service is running' ,
110- version : process . env . npm_package_version || '1.0.0'
111- } ) ;
112- } ) ;
113-
114- // Register all API routes
115- registerRoutes ( app ) ;
116-
117143// Start the server
118144const port = process . env . PORT || 3000 ;
119145serve ( {
0 commit comments