()
| 68 | } |
| 69 | |
| 70 | setupMiddleware() { |
| 71 | // Parse JSON bodies |
| 72 | this.app.use(express.json()) |
| 73 | |
| 74 | // Parse URL-encoded bodies |
| 75 | this.app.use(express.urlencoded({ extended: true })) |
| 76 | |
| 77 | // CORS support |
| 78 | this.app.use((req, res, next) => { |
| 79 | res.header('Access-Control-Allow-Origin', '*') |
| 80 | res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS') |
| 81 | res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Test') |
| 82 | |
| 83 | if (req.method === 'OPTIONS') { |
| 84 | res.status(200).end() |
| 85 | return |
| 86 | } |
| 87 | next() |
| 88 | }) |
| 89 | |
| 90 | // Auto-reload middleware - check if file changed before each request |
| 91 | this.app.use((req, res, next) => { |
| 92 | try { |
| 93 | if (fs.existsSync(this.dbFile)) { |
| 94 | const stats = fs.statSync(this.dbFile) |
| 95 | if (!this.lastModified || stats.mtime > this.lastModified) { |
| 96 | console.log(`[Auto-reload] Database file changed (${this.dbFile}), reloading data...`) |
| 97 | console.log(`[Auto-reload] Old mtime: ${this.lastModified}, New mtime: ${stats.mtime}`) |
| 98 | this.reloadData() |
| 99 | this.lastModified = stats.mtime |
| 100 | console.log(`[Auto-reload] Data reloaded, user name is now: ${this.data.user?.name}`) |
| 101 | } |
| 102 | } |
| 103 | } catch (err) { |
| 104 | console.warn('[Auto-reload] Error checking file modification time:', err.message) |
| 105 | } |
| 106 | next() |
| 107 | }) |
| 108 | |
| 109 | // Logging middleware |
| 110 | this.app.use((req, res, next) => { |
| 111 | console.log(`${req.method} ${req.path}`) |
| 112 | next() |
| 113 | }) |
| 114 | } |
| 115 | |
| 116 | setupRoutes() { |
| 117 | // Reload endpoint (for testing) |
no test coverage detected