-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: document catch-all routes --------- Co-authored-by: Ángel M <[email protected]>
- Loading branch information
1 parent
85309ee
commit 65c71c7
Showing
11 changed files
with
302 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# JavaScript catch-all example | ||
|
||
This worker showcases how catch-all routes can be used by your workers. | ||
|
||
|
||
## Prerequisites | ||
|
||
* Wasm Workers Server (wws): | ||
|
||
```shell-session | ||
curl -fsSL https://workers.wasmlabs.dev/install | bash | ||
``` | ||
|
||
## Run | ||
|
||
```shell-session | ||
wws https://github.com/vmware-labs/wasm-workers-server.git -i --git-folder "examples/js-catchall" | ||
``` | ||
|
||
## Resources | ||
|
||
* [Catch-all routes](https://workers.wasmlabs.dev/docs/features/catch-all-routes) | ||
* [JavaScript documentation](https://workers.wasmlabs.dev/docs/languages/javascript) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Builds a reply to the given request | ||
*/ | ||
const reply = (request) => { | ||
if (request.method != "GET") { | ||
// Don't allow other methods. | ||
// Here you can see how to return a custom status | ||
return new Response("Method not allowed", { | ||
status: 405 | ||
}); | ||
} | ||
|
||
// Body response | ||
const body = `<!DOCTYPE html> | ||
<head> | ||
<title>Wasm Workers Server</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> | ||
<style> | ||
body { max-width: 1000px; } | ||
main { margin: 5rem 0; } | ||
h1, p { text-align: center; } | ||
h1 { margin-bottom: 2rem; } | ||
pre { font-size: .9rem; } | ||
pre > code { padding: 2rem; } | ||
p { margin-top: 2rem; } | ||
</style> | ||
</head> | ||
<body> | ||
<main> | ||
<h1>Hello from Wasm Workers Server 👋</h1> | ||
<pre><code>Replying to ${request.url} | ||
Method: ${request.method} | ||
User Agent: ${request.headers.get("user-agent")} | ||
Payload: ${request.body || "-"}</code></pre> | ||
<p> | ||
<b>This worker is replying to the top-level catch-all endpoint.</b> | ||
</p> | ||
<p> | ||
This page was generated by a JavaScript file running in WebAssembly. | ||
</p> | ||
</main> | ||
</body>`; | ||
|
||
// Build a new response | ||
let response = new Response(body); | ||
|
||
// Add a new header | ||
response.headers.set("x-generated-by", "wasm-workers-server"); | ||
|
||
return response; | ||
} | ||
|
||
// Subscribe to the Fetch event | ||
addEventListener("fetch", event => { | ||
return event.respondWith(reply(event.request)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Builds a reply to the given request | ||
*/ | ||
const reply = (request) => { | ||
if (request.method != "GET") { | ||
// Don't allow other methods. | ||
// Here you can see how to return a custom status | ||
return new Response("Method not allowed", { | ||
status: 405 | ||
}); | ||
} | ||
|
||
// Body response | ||
const body = `<!DOCTYPE html> | ||
<head> | ||
<title>Wasm Workers Server</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> | ||
<style> | ||
body { max-width: 1000px; } | ||
main { margin: 5rem 0; } | ||
h1, p { text-align: center; } | ||
h1 { margin-bottom: 2rem; } | ||
pre { font-size: .9rem; } | ||
pre > code { padding: 2rem; } | ||
p { margin-top: 2rem; } | ||
</style> | ||
</head> | ||
<body> | ||
<main> | ||
<h1>Hello from Wasm Workers Server 👋</h1> | ||
<pre><code>Replying to ${request.url} | ||
Method: ${request.method} | ||
User Agent: ${request.headers.get("user-agent")} | ||
Payload: ${request.body || "-"}</code></pre> | ||
<p> | ||
<b>This worker is replying to the <code>/about</code> endpoint.</b> | ||
</p> | ||
<p> | ||
This page was generated by a JavaScript file running in WebAssembly. | ||
</p> | ||
</main> | ||
</body>`; | ||
|
||
// Build a new response | ||
let response = new Response(body); | ||
|
||
// Add a new header | ||
response.headers.set("x-generated-by", "wasm-workers-server"); | ||
|
||
return response; | ||
} | ||
|
||
// Subscribe to the Fetch event | ||
addEventListener("fetch", event => { | ||
return event.respondWith(reply(event.request)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Builds a reply to the given request | ||
*/ | ||
const reply = (request) => { | ||
if (request.method != "GET") { | ||
// Don't allow other methods. | ||
// Here you can see how to return a custom status | ||
return new Response("Method not allowed", { | ||
status: 405 | ||
}); | ||
} | ||
|
||
// Body response | ||
const body = `<!DOCTYPE html> | ||
<head> | ||
<title>Wasm Workers Server</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> | ||
<style> | ||
body { max-width: 1000px; } | ||
main { margin: 5rem 0; } | ||
h1, p { text-align: center; } | ||
h1 { margin-bottom: 2rem; } | ||
pre { font-size: .9rem; } | ||
pre > code { padding: 2rem; } | ||
p { margin-top: 2rem; } | ||
</style> | ||
</head> | ||
<body> | ||
<main> | ||
<h1>Hello from Wasm Workers Server 👋</h1> | ||
<pre><code>Replying to ${request.url} | ||
Method: ${request.method} | ||
User Agent: ${request.headers.get("user-agent")} | ||
Payload: ${request.body || "-"}</code></pre> | ||
<p> | ||
<b>This worker is replying to the catch-all endpoint under <code>/other</code>.</b> | ||
</p> | ||
<p> | ||
This page was generated by a JavaScript file running in WebAssembly. | ||
</p> | ||
</main> | ||
</body>`; | ||
|
||
// Build a new response | ||
let response = new Response(body); | ||
|
||
// Add a new header | ||
response.headers.set("x-generated-by", "wasm-workers-server"); | ||
|
||
return response; | ||
} | ||
|
||
// Subscribe to the Fetch event | ||
addEventListener("fetch", event => { | ||
return event.respondWith(reply(event.request)); | ||
}); |