@@ -29,10 +29,11 @@ class TestServer {
2929 if ( fs . existsSync ( this . dbFile ) ) {
3030 this . lastModified = fs . statSync ( this . dbFile ) . mtime
3131 }
32- console . log ( 'Loaded data:' , data )
32+ console . log ( '[Data Load] Loaded data from file :' , JSON . stringify ( data ) )
3333 return data
3434 } catch ( err ) {
35- console . warn ( `Could not load data file ${ this . dbFile } :` , err . message )
35+ console . warn ( `[Data Load] Could not load data file ${ this . dbFile } :` , err . message )
36+ console . log ( '[Data Load] Using fallback default data' )
3637 return {
3738 posts : [ { id : 1 , title : 'json-server' , author : 'davert' } ] ,
3839 user : { name : 'john' , password : '123456' } ,
@@ -41,15 +42,23 @@ class TestServer {
4142 }
4243
4344 reloadData ( ) {
45+ console . log ( '[Reload] Reloading data from file...' )
4446 this . data = this . loadData ( )
47+ console . log ( '[Reload] Data reloaded successfully' )
4548 return this . data
4649 }
4750
4851 saveData ( ) {
4952 try {
5053 fs . writeFileSync ( this . dbFile , JSON . stringify ( this . data , null , 2 ) )
54+ console . log ( '[Save] Data saved to file' )
55+ // Force update modification time to ensure auto-reload works
56+ const now = new Date ( )
57+ fs . utimesSync ( this . dbFile , now , now )
58+ this . lastModified = now
59+ console . log ( '[Save] File modification time updated' )
5160 } catch ( err ) {
52- console . warn ( `Could not save data file ${ this . dbFile } :` , err . message )
61+ console . warn ( `[Save] Could not save data file ${ this . dbFile } :` , err . message )
5362 }
5463 }
5564
@@ -79,13 +88,15 @@ class TestServer {
7988 if ( fs . existsSync ( this . dbFile ) ) {
8089 const stats = fs . statSync ( this . dbFile )
8190 if ( ! this . lastModified || stats . mtime > this . lastModified ) {
82- console . log ( 'Database file changed, reloading data...' )
91+ console . log ( `[Auto-reload] Database file changed (${ this . dbFile } ), reloading data...` )
92+ console . log ( `[Auto-reload] Old mtime: ${ this . lastModified } , New mtime: ${ stats . mtime } ` )
8393 this . reloadData ( )
8494 this . lastModified = stats . mtime
95+ console . log ( `[Auto-reload] Data reloaded, user name is now: ${ this . data . user ?. name } ` )
8596 }
8697 }
8798 } catch ( err ) {
88- console . warn ( 'Error checking file modification time:' , err . message )
99+ console . warn ( '[Auto-reload] Error checking file modification time:' , err . message )
89100 }
90101 next ( )
91102 } )
@@ -115,6 +126,7 @@ class TestServer {
115126
116127 // User endpoints
117128 this . app . get ( '/user' , ( req , res ) => {
129+ console . log ( `[GET /user] Serving user data: ${ JSON . stringify ( this . data . user ) } ` )
118130 res . json ( this . data . user )
119131 } )
120132
0 commit comments