SSR Configuration
Angular.json Settings
The SSR configuration is defined inangular.json within the build options:
angular.json
Key Configuration Options
| Option | Value | Description |
|---|---|---|
browser | src/main.ts | Client-side entry point |
server | src/main.server.ts | Server-side bootstrap file |
outputMode | server | Enables server output mode |
ssr.entry | src/server.ts | Express server entry point |
The
outputMode: "server" setting ensures Angular builds the application for SSR instead of static site generation.Server Bootstrap
The server-side bootstrap is configured insrc/main.server.ts:
src/main.server.ts
- Imports the server-specific configuration
- Bootstraps the Angular application with SSR context
- Exports the bootstrap function for the Express server
Express Server Setup
The Express server is configured insrc/server.ts:
src/server.ts
Server Features
- Static File Serving: Serves pre-built browser assets with 1-year cache
- Angular SSR Engine: Handles dynamic rendering of Angular components
- Port Configuration: Defaults to port 4000, configurable via
PORTenvironment variable - API Support: Placeholder for adding Express REST API endpoints
Building and Running SSR
Build the Application
dist/count-app directory.
Serve the SSR Application
Use the npm script to run the SSR server:package.json:
package.json
Environment Variables
Configure the server using environment variables:The server automatically checks if it’s the main module or running via PM2 before starting. This allows the server file to be imported in other contexts without auto-starting.
Adding API Endpoints
You can add Express REST API endpoints insrc/server.ts before the Angular catch-all handler:
Related Pages
- Building - Learn how to build the application for production
- Development Setup - Understanding the project structure and setup