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
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:
See https://developer.mozilla.org/en-US/docs/Web/API/RequestBut I would like to serve a text response and supply that text. How would that work?
Not in the request.
In the server.
Would work the same way:
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.
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
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.π
Because I want to supply html to the handlernot 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..