Hiking-Logbook

API Setup Guide

This guide shows you how to set up the backend API for development.

What You Need

What the API Does

The backend API provides:

Setup Steps

1. Install Dependencies

cd backend
npm install

2. Configure Environment

Create backend/.env file (if not done in database setup):

PORT=3001
NODE_ENV=development

# Firebase credentials (from database setup)
FIREBASE_PROJECT_ID=your_project_id
FIREBASE_PRIVATE_KEY_ID=your_private_key_id
FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----"
FIREBASE_CLIENT_EMAIL=firebase-adminsdk-xxxxx@your_project.iam.gserviceaccount.com
FIREBASE_CLIENT_ID=your_client_id

3. Start the Server

npm start

You should see:

Server running on port 3001
Environment: development
Health check: http://localhost:3001/health
API Docs: http://localhost:3001/api-docs

4. Test the API

Open your browser and visit:

You should see the API is running!

Available Scripts

npm start              # Start the server
npm run dev            # Start with auto-reload (nodemon)
npm test               # Run tests
npm run test:coverage  # Run tests with coverage
npm run lint           # Check code style
npm run lint:fix       # Fix code style issues

Project Structure

backend/
├── src/
│   ├── server.js          # Main server file
│   ├── config/            # Configuration files
│   ├── middleware/        # Authentication, error handling
│   ├── routes/            # API endpoints
│   ├── services/          # Business logic
│   ├── models/            # Database schemas
│   └── tests/             # Test files
├── package.json           # Dependencies
└── .env                   # Environment variables

Main API Routes

The API provides these endpoints:

For detailed documentation:

Development Workflow

Adding a New Feature

  1. Create route file in src/routes/
  2. Create service file in src/services/
  3. Add route to src/server.js
  4. Write tests in src/tests/
  5. Test locally with Swagger or Postman

Example: Testing an Endpoint

# Test health endpoint
curl http://localhost:3001/health

# Test public stats (no auth)
curl http://localhost:3001/api/public/stats

# Test with authentication
curl -H "Authorization: Bearer YOUR_TOKEN" \
     http://localhost:3001/api/users/profile

Common Issues

Problem: Port 3001 already in use

# Solution: Kill the process
npx kill-port 3001

Problem: Firebase connection error

Problem: “Cannot find module” errors

# Solution: Reinstall dependencies
rm -rf node_modules package-lock.json
npm install

Problem: CORS errors from frontend

Testing

Run tests to verify everything works:

# Run all tests
npm test

# Run with coverage
npm run test:coverage

# Run specific test file
npm test -- auth.test.js

Next Steps


The frontend will connect to http://localhost:3001