fix: resolve JSON.parse error in frontend API calls
- Add error handling for JSON encoding in all backend handlers - Add CORS support to backend server - Add proxy configuration for frontend development server - Improve error handling in frontend API calls - Build frontend to create static files for backend serving - Add comprehensive error messages and user feedback Fixes issue where frontend received malformed JSON responses from backend API.
This commit is contained in:
28
backend/test/test_api.sh
Executable file
28
backend/test/test_api.sh
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/nix/store/ypgcdmzzlgnrmdcsq72c3dxz651jg9zc-bash-5.3p3/bin/bash
|
||||
|
||||
# Start the server in the background
|
||||
./markdown-editor --data-dir ./test/data --port 8081 --host 127.0.0.1 &
|
||||
SERVER_PID=$!
|
||||
|
||||
# Give the server time to start
|
||||
sleep 2
|
||||
|
||||
# Test the /api/files endpoint
|
||||
echo "Testing GET /api/files..."
|
||||
response=$(curl -s http://127.0.0.1:8081/api/files)
|
||||
echo "Response: $response"
|
||||
echo ""
|
||||
|
||||
# Test if it's valid JSON
|
||||
if echo "$response" | python3 -m json.tool > /dev/null 2>&1; then
|
||||
echo "✓ Response is valid JSON"
|
||||
else
|
||||
echo "✗ Response is NOT valid JSON"
|
||||
echo "Response was: $response"
|
||||
fi
|
||||
|
||||
# Kill the server
|
||||
kill $SERVER_PID 2>/dev/null
|
||||
wait $SERVER_PID 2>/dev/null
|
||||
|
||||
echo "Test completed"
|
||||
35
backend/test/test_api_detailed.sh
Executable file
35
backend/test/test_api_detailed.sh
Executable file
@@ -0,0 +1,35 @@
|
||||
#!/nix/store/ypgcdmzzlgnrmdcsq72c3dxz651jg9zc-bash-5.3p3/bin/bash
|
||||
|
||||
# Start the server in the background
|
||||
./markdown-editor --data-dir ./test/data --port 8082 --host 127.0.0.1 &
|
||||
SERVER_PID=$!
|
||||
|
||||
# Give the server time to start
|
||||
sleep 2
|
||||
|
||||
# Test the /api/files endpoint
|
||||
echo "Testing GET /api/files..."
|
||||
response=$(curl -s -i http://127.0.0.1:8082/api/files)
|
||||
echo "$response"
|
||||
echo ""
|
||||
|
||||
# Extract just the body (skip headers)
|
||||
body=$(echo "$response" | awk 'NR>1 && $0 !~ /^[A-Za-z]/ {print; next} /^[A-Za-z]/ {exit}')
|
||||
echo "Body: '$body'"
|
||||
echo ""
|
||||
|
||||
# Test if it's valid JSON
|
||||
echo "Checking if response is valid JSON..."
|
||||
if echo "$body" | node -e "console.log(JSON.parse(require('fs').readFileSync(0, 'utf8')))" > /dev/null 2>&1; then
|
||||
echo "✓ Response is valid JSON"
|
||||
echo "$body" | node -e "console.log(JSON.parse(require('fs').readFileSync(0, 'utf8')))"
|
||||
else
|
||||
echo "✗ Response is NOT valid JSON"
|
||||
echo "Response was: '$body'"
|
||||
fi
|
||||
|
||||
# Kill the server
|
||||
kill $SERVER_PID 2>/dev/null
|
||||
wait $SERVER_PID 2>/dev/null
|
||||
|
||||
echo "Test completed"
|
||||
Reference in New Issue
Block a user