Skip to content

Commit 54adbc0

Browse files
Copilotkobenguyent
andcommitted
Fix data reload timing issue in test server for GET user test
Co-authored-by: kobenguyent <[email protected]>
1 parent 1d2d186 commit 54adbc0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lib/test-server.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class TestServer {
1313
this.port = config.port || 8010
1414
this.host = config.host || 'localhost'
1515
this.dbFile = config.dbFile || path.join(__dirname, '../test/data/rest/db.json')
16+
this.lastModified = null
1617
this.data = this.loadData()
1718

1819
this.setupMiddleware()
@@ -24,6 +25,10 @@ class TestServer {
2425
try {
2526
const content = fs.readFileSync(this.dbFile, 'utf8')
2627
const data = JSON.parse(content)
28+
// Update lastModified time when loading data
29+
if (fs.existsSync(this.dbFile)) {
30+
this.lastModified = fs.statSync(this.dbFile).mtime
31+
}
2732
console.log('Loaded data:', data)
2833
return data
2934
} catch (err) {
@@ -68,6 +73,23 @@ class TestServer {
6873
next()
6974
})
7075

76+
// Auto-reload middleware - check if file changed before each request
77+
this.app.use((req, res, next) => {
78+
try {
79+
if (fs.existsSync(this.dbFile)) {
80+
const stats = fs.statSync(this.dbFile)
81+
if (!this.lastModified || stats.mtime > this.lastModified) {
82+
console.log('Database file changed, reloading data...')
83+
this.reloadData()
84+
this.lastModified = stats.mtime
85+
}
86+
}
87+
} catch (err) {
88+
console.warn('Error checking file modification time:', err.message)
89+
}
90+
next()
91+
})
92+
7193
// Logging middleware
7294
this.app.use((req, res, next) => {
7395
console.log(`${req.method} ${req.path}`)

0 commit comments

Comments
 (0)