@@ -9,14 +9,39 @@ const __filename = fileURLToPath(import.meta.url);
99const __dirname = path . dirname ( __filename ) ;
1010const app = express ( ) ;
1111
12+ // Simple structured logger with optional DEBUG
13+ const LOG_LEVEL = ( process . env . LOG_LEVEL || 'info' ) . toLowerCase ( ) ;
14+ const isDebug = LOG_LEVEL === 'debug' || LOG_LEVEL === 'trace' || process . env . DEBUG === '1' ;
15+ const ts = ( ) => new Date ( ) . toISOString ( ) ;
16+ const info = ( ...args ) => console . log ( ts ( ) , '[INFO]' , ...args ) ;
17+ const warn = ( ...args ) => console . warn ( ts ( ) , '[WARN]' , ...args ) ;
18+ const error = ( ...args ) => console . error ( ts ( ) , '[ERROR]' , ...args ) ;
19+ const debug = ( ...args ) => { if ( isDebug ) console . log ( ts ( ) , '[DEBUG]' , ...args ) ; } ;
20+
1221const ROOT = __dirname ;
1322const CONFIG_DIR = path . join ( ROOT , 'config' ) ;
1423const PHOTO_DIR = path . join ( CONFIG_DIR , 'photos' ) ;
1524const FAVICON_DIR = path . join ( ROOT , 'favicon' ) ;
1625const ALLOWED = new Set ( [ '.jpg' , '.jpeg' , '.png' , '.webp' , '.avif' ] ) ;
1726
27+ // Request/response logger with timing
28+ app . use ( ( req , res , next ) => {
29+ const start = process . hrtime . bigint ( ) ;
30+ debug ( 'Incoming request' , { method : req . method , url : req . originalUrl || req . url , ip : req . ip } ) ;
31+ res . on ( 'finish' , ( ) => {
32+ const durMs = Number ( process . hrtime . bigint ( ) - start ) / 1e6 ;
33+ const msg = `${ req . method } ${ req . originalUrl || req . url } -> ${ res . statusCode } ${ Math . round ( durMs ) } ms` ;
34+ if ( res . statusCode >= 500 ) error ( msg ) ;
35+ else if ( res . statusCode >= 400 ) warn ( msg ) ;
36+ else info ( msg ) ;
37+ } ) ;
38+ next ( ) ;
39+ } ) ;
40+
1841app . use ( express . static ( ROOT , { extensions : [ 'html' ] , etag : true , lastModified : true } ) ) ;
1942app . use ( '/config' , express . static ( CONFIG_DIR , { etag : true , lastModified : true } ) ) ;
43+ // Serve photos from config/photos under a stable /photos path used by the client
44+ app . use ( '/photos' , express . static ( PHOTO_DIR , { etag : true , lastModified : true } ) ) ;
2045// Serve favicons and related assets
2146app . use ( '/favicon' , express . static ( FAVICON_DIR , { etag : true , lastModified : true } ) ) ;
2247app . get ( '/favicon.ico' , ( _req , res ) => res . sendFile ( path . join ( FAVICON_DIR , 'wedding_bell_favicon.ico' ) ) ) ;
@@ -25,17 +50,29 @@ app.get('/site.webmanifest', (_req, res) => res.sendFile(path.join(FAVICON_DIR,
2550
2651app . get ( '/api/photos' , async ( _req , res ) => {
2752 try {
53+ debug ( 'Listing photos from' , PHOTO_DIR , 'allowed extensions:' , [ ...ALLOWED ] . join ( ',' ) ) ;
2854 const primary = await readdir ( PHOTO_DIR , { withFileTypes : true } ) ;
2955 const files = primary
3056 . filter ( f => f . isFile ( ) && ALLOWED . has ( extname ( f . name ) . toLowerCase ( ) ) )
3157 . map ( f => f . name )
3258 . sort ( ( a , b ) => a . localeCompare ( b , undefined , { numeric : true } ) ) ;
59+ info ( `/api/photos -> ${ files . length } files` ) ;
3360 res . set ( 'Cache-Control' , 'no-store' ) ;
3461 res . json ( { files } ) ;
3562 } catch ( e ) {
63+ // If the photos directory doesn't exist yet, treat as empty set
64+ if ( e && ( e . code === 'ENOENT' || e . code === 'ENOTDIR' ) ) {
65+ warn ( 'Photos directory not found; returning empty list' ) ;
66+ return res . json ( { files : [ ] } ) ;
67+ }
68+ error ( 'Failed to list photos:' , e && e . stack ? e . stack : e ) ;
3669 res . status ( 500 ) . json ( { error : e . message } ) ;
3770 }
3871} ) ;
3972
4073const port = process . env . PORT || 5500 ;
41- app . listen ( port , ( ) => console . log ( `Wedding site running at http://localhost:${ port } ` ) ) ;
74+ app . listen ( port , ( ) => {
75+ info ( `Wedding site running at http://localhost:${ port } ` ) ;
76+ info ( 'Environment' , { node : process . versions . node , platform : process . platform , arch : process . arch , logLevel : LOG_LEVEL } ) ;
77+ info ( 'Paths' , { ROOT , CONFIG_DIR , PHOTO_DIR , FAVICON_DIR } ) ;
78+ } ) ;
0 commit comments