The test/server_api_test.js file was created to catch route ordering bugs in Express that could cause API endpoints to be intercepted by catch-all routes.
In the initial implementation, the /api/instance endpoint was defined after the catch-all route:
// ❌ WRONG ORDER (causes bug)
app.use(express.static(distPath));
app.get(/^.*$/, (_req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
app.get('/api/instance', (req, res) => { // This never gets hit!
res.json({ instanceId, isFlyInstance });
});This caused:
GET /api/instance→ Returnedindex.html(❌)- Status: 200 OK
- Content-Type:
text/htmlinstead ofapplication/json - Response body: HTML instead of JSON
API routes must be defined before the catch-all:
// ✅ CORRECT ORDER
app.get('/api/instance', (req, res) => {
res.json({ instanceId, isFlyInstance });
});
app.use(express.static(distPath));
app.get(/^.*$/, (_req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});Tests 8 different assertions:
- ✅ Status code is 200
- ✅ Content-Type is
application/json - ✅ Response is NOT HTML
- ✅ Response is valid JSON
- ✅ Has
instanceIdfield - ✅ Has
isFlyInstancefield - ✅
instanceIdvalue is correct - ✅
isFlyInstancevalue is correct
This is the key test that would have caught the bug!
Verifies that non-API routes (like /some-page) still serve the HTML SPA correctly.
Intentionally creates the buggy route order and verifies that the test would detect it. This proves the test is effective.
Tests that having multiple API endpoints doesn't cause interference:
/api/instance/api/health/api/rooms
Run all tests:
npm testRun only server API tests:
node test/server_api_test.js--- Starting Server API Endpoint Tests ---
Test 1: /api/instance Returns JSON (Not HTML)
✅ Passed: Status code is 200
✅ Passed: Content-Type is application/json
✅ Passed: Response is not HTML
✅ Passed: Response is valid JSON
✅ Passed: Has 'instanceId' field
✅ Passed: Has 'isFlyInstance' field
✅ Passed: instanceId matches expected value
✅ Passed: isFlyInstance matches expected value
Test 2: Catch-All Still Works for Non-API Routes
✅ Passed: Catch-all serves HTML for non-API routes
Test 3: Verify Test Detects Wrong Route Order (Bug Simulation)
✅ Passed: Test correctly detects wrong route order (returns HTML)
This confirms the test would have caught the bug!
Test 4: Multiple API Endpoints Work
✅ Passed: Multiple API endpoints work correctly
--- All Server API Tests Passed ---
🎯 These tests would have caught the route ordering bug!
- Prevents Regression: Route ordering bugs won't happen again
- Documents Expected Behavior: Clear what the API should return
- Fast Feedback: Catches issues before deployment
- Comprehensive: Tests multiple scenarios and edge cases
- Self-Verifying: Includes a test that proves it would catch the bug
The test is now part of the main test suite in package.json:
{
"scripts": {
"test:unit": "... && node test/server_api_test.js"
}
}Every npm test run will verify API endpoints work correctly!