forked from Badgerati/Pode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-route-endpoints.ps1
37 lines (28 loc) · 1.21 KB
/
web-route-endpoints.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
Import-Module "$($path)/src/Pode.psm1" -Force -ErrorAction Stop
# or just:
# Import-Module Pode
# create a server, and start listening on port 8080 and 8443
Start-PodeServer {
# listen on localhost:8080
Add-PodeEndpoint -Address 127.0.0.1 -Port 8080 -Protocol Http -Name Endpoint1
Add-PodeEndpoint -Address 127.0.0.2 -Port 8080 -Protocol Http -Name Endpoint2
# set view engine to pode
Set-PodeViewEngine -Type Pode
# GET request for web page
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
Write-PodeViewResponse -Path 'simple' -Data @{ 'numbers' = @(1, 2, 3); }
}
# GET request to download a file
Add-PodeRoute -Method Get -Path '/download' -ScriptBlock {
Set-PodeResponseAttachment -Path 'Anger.jpg'
}
# GET request with parameters
Add-PodeRoute -Method Get -Path '/:userId/details' -ScriptBlock {
Write-PodeJsonResponse -Value @{ 'userId' = $WebEvent.Parameters['userId'] }
}
# ALL requests for 127.0.0.2 to 127.0.0.1
Add-PodeRoute -Method * -Path * -EndpointName Endpoint2 -ScriptBlock {
Move-PodeResponseUrl -Address 127.0.0.1
}
}