The response object contains information to be sent back to the user such as “body”, “headers”, “status”, “type”, and the method redirect( ).

 

According to Oak framework GitHub page, redirect( ) method is “a method to simplify redirecting the response to another URL. It will set the Location header to the supplied url and the status to 302 Found (unless the status is already a 3XX status). The use of symbol REDIRECT_BACK as the url indicates that the Referer header in the request should be used as the direction, with the alt being the alternative location if the Referer is not set. If neither the alt nor the Referer are set, the redirect will occur to /. A basic HTML (if the requestor supports it) or a text body will be set explaining they are being redirected.”

 

“When the response Content-Type is not set in the headers of the .response, oak will automatically try to determine the appropriate Content-Type. First it will look at .response.type. If assigned, it will try to resolve the appropriate media type based on treating the value of .type as either the media type, or resolving the media type based on an extension. For example if .type was set to "html", then the Content-Type will be set to "text/html".”

 

“If .type is not set with a value, then oak will inspect the value of .response.body. If the value is a string, then oak will check to see if the string looks like HTML, if so, Content-Type will be set to text/html otherwise it will be set to text/plain. If the value is an object, other than a Uint8Array, a Deno.Reader, or null, the object will be passed to JSON.stringify() and the Content-Type will be set to application/json.”

 

“If the value of body is a function, the function will be called with no arguments. If the return value of the function is promise like, that will be await, and the resolved value will be processed as above. If the value is not promise like, it will be processed as above.”

 

 // app.js

import { Application, Router } from "https://deno.land/x/oak/mod.ts"
import html from "./views/index.jsx"

const app = new Application({keys: ["secret1"]})
const router = new Router()

router.get('/', async (ctx) => {
  ctx.response.body = html
})
 
app.use(router.routes())
app.use(router.allowedMethods())

app.use(async (context, next) => {
  try {
    await context.send({
      root: `${Deno.cwd()}/static`,
      index: "index.html",
    });
  } catch {
    await next()
  }
})

app.addEventListener(
  "listen",
  (e) => console.log("Listening on http://localhost:8000")
)

await app.listen({ port: 8000})

// denon run --allow-net --allow-read app.js

 

// views/index.jsx

/** @jsx h */
import { h, renderSSR } from "https://deno.land/x/nano_jsx@v0.0.33/mod.ts"

function Index(props){
  return(
    <html>
      <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <title>Oak Web Framework</title>
      </head>
      <body>
        <h3>{props.message}</h3>
      </body>
    </html>
  )
}

const message = `Here is HTML content for the response body.`
const str = renderSSR(<Index message={message} />)
const html = `<!DOCTYPE html>${str}`

export default html