This repository contains a Node.js server built with Express and TypeScript. It follows a structured architecture with service, controller, and middleware layers. The server includes token-based authentication and logging configured to automatically switch between JSON logging (for production) and console-based logging (for local development). Logs are stored and can be accessed via an endpoint. In production, logs are stored in a volume to persist across redeploys.
- Express.js as the web framework
- TypeScript for type safety
- Token-based authentication
- Service-Controller architecture
- Middleware support
- Logging:
- JSON logging in production
- Text-based logging in local development
- Logs stored in a text file
- Endpoint to download logs
- Automatic switching between logging formats
- Production logs persist across redeploys using a volume
# Clone the repository git clone <repo-url> cd <repo-folder> # Install dependencies npm installCreate a .env file in the root directory and configure the following variables:
PORT=3000 NODE_ENV=development # Change to 'production' in production HEALTH_SECRET=your_secret_herenpm run devThis runs the server with ts-node-dev and text-based logging.
npm run build npm startThis compiles TypeScript and runs the server with JSON logging.
/src │── controllers # Handles HTTP requests and responses │── services # Business logic and database interactions │── middleware # Express middleware (e.g., authentication, logging) │── utils # Helper functions │── config # Configuration files │── app.ts # Main Express server setup │── server.ts # Server entry point To access protected routes, include a valid token in the Authorization header.
Logging automatically switches based on the NODE_ENV:
- Development: Logs in text format for readability and stored in a text file (
logs/server.log). - Production: Logs in JSON format for structured logging, stored in a volume to persist across redeploys.
- Fork the repository.
- Create a new branch.
- Make your changes and commit them.
- Push to your fork and submit a pull request.
This project is licensed under the MIT License.