Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
suddenlyGiovanni and github-actions[bot] authored Dec 18, 2024
1 parent c5ec44e commit 32e2d82
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions apps/web/server/server.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,60 @@
# Server Setup Documentation

## Overview

The server is an Express application that serves a React Router build. It supports both development and production environments, dynamically configuring the server port and host.

## Key Components

### Configuration (`Config` class)

- Manages environment variables and server build path.
- Uses `effect` library for schema validation.

### Source Map Support

- Uses `source-map-support` to enhance error stack traces.

### Express Application

- Uses `compression` for response compression.
- Uses `morgan` for HTTP request logging.
- Serves static files and handles all incoming requests.

### Development Mode

- Uses `vite` for development server with middleware mode.
- Dynamically loads and handles requests using Vite's SSR capabilities.

### Production Mode

- Serves static assets with caching.
- Loads the server build from the specified path.

### Request Handling

- Uses `react-router` for handling requests and responses.
- Custom request handler created using `react-router-express`.

## Middleware Behavior

### Compression Middleware (`compression`)

- Compresses response bodies for all requests.
- Adds `Content-Encoding` headers and compresses the body.

### Morgan Middleware (`morgan`)

- Logs HTTP requests.
- Does not modify the `Request` or `Response` but logs details like method, URL, status, and response time.

### Static File Serving (`express.static`)

- Serves static files from the specified directories.
- Adds appropriate headers (e.g., `Cache-Control`) and serves files.

### Vite Dev Server Middleware (`viteDevServer.middlewares`)

- Handles requests in development mode.
- Attaches custom middlewares and acts as a handler function for custom HTTP servers.
- Provides features like HMR, module loading, and development-specific optimizations.
Expand All @@ -57,9 +68,11 @@ The server is an Express application that serves a React Router build. It suppor
## `viteDevServer.ssrLoadModule('./server/app.ts')` and `app.ts`

### `ssrLoadModule`

- Loads a given URL as an instantiated module for SSR.

### `app.ts`

- Sets up the Express application and request handler.
- Defines a request handler using `createRequestHandler` from `react-router-express`.
- Configures the request handler to load the server build and provide context values.
Expand All @@ -72,15 +85,19 @@ The server is an Express application that serves a React Router build. It suppor
## Additional Details

### Configuration

- Dynamically configures the port and host based on environment variables or default settings.

### Source Map Support

- Enhances error stack traces by retrieving source maps for better debugging.

### Request Handling

- Uses `react-router` to handle requests and responses, providing SSR capabilities.

## External Dependencies

- `compression`: Middleware for response compression.
- `effect`: Library for schema validation.
- `express`: Web framework for building the server.
Expand All @@ -93,6 +110,7 @@ The server is an Express application that serves a React Router build. It suppor
# Unit Tests

## Objectives

- Validate the configuration schema and default values.
- Ensure the server starts correctly in both development and production modes.
- Verify middleware and request handling logic.
Expand All @@ -102,25 +120,31 @@ The server is an Express application that serves a React Router build. It suppor
## Test Cases

### Configuration Tests

- Validate default values for `NODE_ENV`, `PORT`, and `HOST`.
- Test schema validation for different configurations.

### Server Initialization Tests

- Ensure the server starts on the specified port and host.
- Verify the correct middleware is applied based on the environment.

### Middleware Tests

- Test `compression` middleware for response compression.
- Verify `morgan` logs HTTP requests correctly.

### Request Handling Tests

- Test request handling in development mode using Vite.
- Verify request handling in production mode with static file serving.

### Static File Serving Tests

- Ensure static assets are served with correct caching headers.
- Test serving of client-side assets from the build directory.

### Error Handling Tests

- Verify error handling and logging for different scenarios.
- Test source map support for enhanced error stack traces.

0 comments on commit 32e2d82

Please sign in to comment.