HTML Templates & Files

Render dynamic HTML or return files with proper headers.

Full source

File: pwsh/tutorial/examples/9.4-Html-Files.ps1


<#
    Sample: HTML Templates & File Downloads
    Purpose: Demonstrate HTML templating and file download in a Kestrun server.
    File:    9.4-Html-Files.ps1
    Notes:   Shows HTML file, inline template, and file download routes.
#>
param(
    [int]$Port = 5000,
    [IPAddress]$IPAddress = [IPAddress]::Loopback
)
# 1. Logging
New-KrLogger | Add-KrSinkConsole | Register-KrLogger -Name 'console' -SetAsDefault

# 2. Server
New-KrServer -Name 'Responses 9.4'

# 3. Listener
Add-KrEndpoint -IPAddress $IPAddress -Port $Port


# Finalize configuration
Enable-KrConfiguration

# HTML file route
Add-KrMapRoute -Pattern '/page' -Verbs GET -ScriptBlock {
    Write-KrHtmlResponse -FilePath 'Assets/wwwroot/about.html' -StatusCode 200 -Variables @{ Title = 'About'; Body = 'Injected body variable' }
}

# Inline template route
Add-KrMapRoute -Pattern '/inline' -Verbs GET -ScriptBlock {
    $tpl = '<html><body><h1></h1><p></p></body></html>'
    Write-KrHtmlResponse -Template $tpl -Variables @{ Title = 'Inline'; Body = 'Rendered via template var expansion' }
}

# File download route
Add-KrMapRoute -Pattern '/download' -Verbs GET -ScriptBlock {
    Write-KrFileResponse -FilePath 'Assets/wwwroot/files/sample.txt' -FileDownloadName 'report.txt' -ContentDisposition Attachment
}

# Start the server
Start-KrServer -CloseLogsOnExit

Step-by-step

  1. Logging: Register console logger as default.
  2. Server: Create server named ‘Responses 9.4’.
  3. Listener: Listen on 127.0.0.1:5000.
  4. /page: renders HTML file with variable substitution.
  5. /inline: renders inline template string.
  6. /download: sends file with attachment disposition & custom filename.
  7. Enable configuration and start server.

Try it

curl http://127.0.0.1:5000/page
curl http://127.0.0.1:5000/inline
curl -I http://127.0.0.1:5000/download
curl http://127.0.0.1:5000/download --output report.txt

PowerShell equivalents:

Invoke-WebRequest -Uri http://127.0.0.1:5000/page    | Select -Expand Content
Invoke-WebRequest -Uri http://127.0.0.1:5000/inline  | Select -Expand Content
Invoke-WebRequest -Uri http://127.0.0.1:5000/download -Method Head | Select -Expand RawContent
Invoke-WebRequest -Uri http://127.0.0.1:5000/download -OutFile report.txt

Example routes

# Logging
New-KrLogger | Add-KrSinkConsole | Register-KrLogger -Name 'console' -SetAsDefault

# Server
New-KrServer -Name 'Responses 9.4'

# Listener
Add-KrEndpoint -IPAddress '127.0.0.1' -Port 5000

# Enable configuration
Enable-KrConfiguration

# HTML file route
Add-KrMapRoute -Pattern '/page' -Verbs GET -ScriptBlock {
    Write-KrHtmlResponse -FilePath 'Assets/wwwroot/about.html' -StatusCode 200 -Variables @{ Title = 'About'; Body = 'Injected body variable' }
}

# Inline template route
Add-KrMapRoute -Pattern '/inline' -Verbs GET -ScriptBlock {
    $tpl = '<html><body><h1></h1><p></p></body></html>'
    Write-KrHtmlResponse -Template $tpl -Variables @{ Title = 'Inline'; Body = 'Rendered via template var expansion' }
}

# File download route
Add-KrMapRoute -Pattern '/download' -Verbs GET -ScriptBlock {
    Write-KrFileResponse -FilePath 'Assets/wwwroot/files/sample.txt' -FileDownloadName 'report.txt' -ContentDisposition Attachment
}

# Start the server
Start-KrServer

Troubleshooting

Symptom Cause Fix
File not found Wrong path Use Resolve-KrPath or verify file path
Variables not replaced Missing -Variables map Pass hashtable with keys used in template
Wrong disposition header Incorrect flag Use -ContentDisposition Attachment

References


Previous / Next

Previous: Binary & Stream Next: Special Formats (BSON / CBOR)