A Node.js Express API server with authentication, protected routes, and PostgreSQL database integration.
- Node.js with Express.js
- Better Auth for authentication (email/password)
- PostgreSQL database with Drizzle ORM
- CORS configured for frontend integration
- Docker support for database setup
├── auth/ # Authentication configuration
├── data/ # Mock data (JSON files)
├── db/ # Database schema and utilities
├── docker/ # Docker setup for PostgreSQL
├── routes/ # API routes
├── utils/ # Utility functions and middleware
├── index.js # Main server file
├── package.json # Dependencies and scripts
└── .env.example # Environment variables template
- Node.js (v18 or higher)
- Docker and Docker Compose
npm installcp .env.example .env
# Edit .env if needed (defaults should work for development)cd docker && docker compose up -d && cd ..
# This will start PostgreSQL with the default configurationOr from npm script:
npm run db:upMore commands are available in package.json for managing the docker setup. For detailed database setup instructions, see docker/README.md.
The Docker setup includes Adminer web interface for database management at http://localhost:8080.
Use it to browse Better Auth tables, run queries, and debug authentication. See docker/README.md for login credentials and detailed usage.
npm run devThe API server will be available at: http://localhost:3001
- User registration and login
- Email/password authentication
- Session-based authentication with cookies
- Protected routes and API endpoints
- Automatic session management
- Authenticated property listings
- Individual property detail views
- Modal-based property details
- Mock data system with JSON files
- Comprehensive logging for authentication flows
- CORS configured for frontend integration
- Hot reloading during development
- Environment-based configuration
POST /api/auth/sign-up/email- User registrationPOST /api/auth/sign-in/email- User loginPOST /api/auth/sign-out- User logoutGET /api/auth/get-session- Get current session
GET /api/properties- Get all propertiesGET /api/properties/:id- Get single property details
GET /api/status/health- Health checkGET /api/status/info- Server information
Mock data is stored in JSON files for easy management:
data/properties.json- Basic property listingsdata/property-details.json- Detailed property information
You can edit these files directly to modify the available properties without touching the code.
curl -X POST http://localhost:3001/api/auth/sign-up/email \
-H "Content-Type: application/json" \
-H "Origin: http://localhost:3000" \
-d '{
"email": "test@example.com",
"password": "password123",
"name": "Test User"
}'curl -X POST http://localhost:3001/api/auth/sign-in/email \
-H "Content-Type: application/json" \
-H "Origin: http://localhost:3000" \
-d '{
"email": "test@example.com",
"password": "password123"
}'curl http://localhost:3001/api/status/health// In routes/index.js
app.get('/api/your-endpoint', protectedEndpointLogging, requireAuth, yourHandler);The requireAuth middleware is available from auth/auth.js and automatically:
- Verifies user sessions
- Adds user data to
req.user - Returns 401 for unauthenticated requests
For frontend applications, make authenticated requests with:
fetch('http://localhost:3001/api/endpoint', {
credentials: 'include', // Important for cookie auth
});