Custom body

I checked out the example for writing a simple HTTP server to only serve text at this link. How would I write a handler function that accepts a string parameter for the body content? Am I being stupid here?
8 Replies
marvinh.
marvinh.β€’2mo ago
The handler function is typically the function that is passed into Deno.serve and only ever receives the Request object and a second connection info parameter. The handler function is called by Deno.serve under the hood and cannot be changed. If you want to read the request body as text, you can do so by doing:
Deno.serve(async req => {
const text = await req.text()
console.log(text)
return new Response("ok")
});
Deno.serve(async req => {
const text = await req.text()
console.log(text)
return new Response("ok")
});
See https://developer.mozilla.org/en-US/docs/Web/API/Request
πŸŽ€π”Έβ„•π”Ύπ”Όπ•ƒ π”»π•†π•ƒπ•ƒπ”½π”Έβ„‚π”ΌπŸŽ€πŸ‡΅πŸ‡Έ
But I would like to serve a text response and supply that text. How would that work? Not in the request. In the server.
marvinh.
marvinh.β€’2mo ago
Would work the same way:
const SERVER_TEXT = "foobar"

Deno.serve(async req => {
return new Response(`ok ${SERVER_TEXT}`)
});
const SERVER_TEXT = "foobar"

Deno.serve(async req => {
return new Response(`ok ${SERVER_TEXT}`)
});
πŸŽ€π”Έβ„•π”Ύπ”Όπ•ƒ π”»π•†π•ƒπ•ƒπ”½π”Έβ„‚π”ΌπŸŽ€πŸ‡΅πŸ‡Έ
I would like to dynamically build the HTML string and then pass that to the serve function Deno has. Global variables are not an option.
marvinh.
marvinh.β€’2mo ago
What's stopping you from doing that? You can call any functions from within the serve callback and build whatever your heart desires on the fly
Deno.serve(async () => {
const html = dynamicallyBuildHTML();
return new Response(html, {
headers: {Β "Content-Type": "text/html; charset=utf-8" }
})
});
Deno.serve(async () => {
const html = dynamicallyBuildHTML();
return new Response(html, {
headers: {Β "Content-Type": "text/html; charset=utf-8" }
})
});
πŸŽ€π”Έβ„•π”Ύπ”Όπ•ƒ π”»π•†π•ƒπ•ƒπ”½π”Έβ„‚π”ΌπŸŽ€πŸ‡΅πŸ‡Έ
So I cannot supply the html as a parameter? Because I want to supply html to the handler. That cannot be done? Sorry for the late response, busy day.πŸ™ˆ
crowlKats
crowlKatsβ€’5w ago
Because I want to supply html to the handler
not sure I understand; what would that do?
πŸŽ€π”Έβ„•π”Ύπ”Όπ•ƒ π”»π•†π•ƒπ•ƒπ”½π”Έβ„‚π”ΌπŸŽ€πŸ‡΅πŸ‡Έ
I wanna have two functions: One that builds an html string and a function that serves that html and that last function takes the html as an argument. If a certain event happens, the html changes and the function that generates the html is invoked again and that html is passed to the serving function again..
More Posts
Deno.serve: Is it possible to flush a streaming response?I've been playing around with this: https://docs.deno.com/examples/http-server-streaming I am cominHow to mock modules like you can with jest?With node/jest I was able to mock specific functions from modules using `jest.mock` at the start of Should I be using memo() in Fresh (/components/ folder)?If Fresh renders the files in /components/ folder as html, and returns no javascript, should I ever How to use new FormData constructor in Deno server, I want to use it for my supabase edge function.I've created an edge function in supabase that requires formdata,I know it's a browser side thing anHow to import qstash into deno edge function?Hi, simple question, I think, but I can't figure out. I want to import this module into my deno edgDeno Fresh: client side check dev or productionFor my Deno Fresh app, I want to do a simple check from the client-side code to see whether it's in Deno is not definedIs "Deno" supposed to be defined on the front-end client? I'm trying to get a simple app working usshebang and no file extension: how do I turn Typescript on?I have scripts with a `#!/usr/bin/env deno run` shebang, but they execute as javascript β€”adding typeWhy cannot use emoji in variable name?`The module's source code could not be parsed: Unexpected character 'πŸ’Ώ' at file:///Users/`caching known dependencies on devcontainer startupπŸ‘‹ after setting up an ubuntu devcontainer with deno and the vscode extension installed, is it possi