How to modify response information in Deno's Oak

overview

In Deno’s Oak framework, request processing is done within middleware functions. A context object is passed as an argument to the middleware function. Use this context object to retrieve data related to the request, generate the response, and so on.

This section describes how to modify the response information in Deno’s Oak, using the following versions:

Deno v1.12.0
Oak v8.0.0

Also, all the code I’ve created so far is listed below. https://github.com/wiblok/Deno-Oak

How to edit response information

The context object’s response property is used to construct the response to the client. Used in Oak route handlers to set response headers, status codes, data, etc.

The sample code below is an example for understanding the response property of the context object in Deno’s Oak. It takes GET requests and generates and returns different kinds of responses.

response.ts

import { Application, Router } from "https://deno.land/x/oak/mod.ts";

const router = new Router();

router.get("/json", (context) => {
  const jsonResponse = {
    message: "This is a JSON response",
  };
  context.response.body = jsonResponse;
});

router.get("/html", (context) => {
  const htmlResponse = "<h1>This is an HTML response</h1>";
  context.response.headers.set("Content-Type", "text/html");
  context.response.body = htmlResponse;
});

router.get("/header", (context) => {
  context.response.headers.set("Custom-Header", "Custom Value");
  context.response.body = { message: "Check the headers!" };
});

router.get("/error", (context) => {
  const errorResponse = {
    error: "An error occurred",
  };
  context.response.status = 500;
  context.response.body = errorResponse;
});

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 3000 });

Commentary

This source code creates a Deno Oak server and serves different endpoints.

  • Import Oak module and create router:
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const router = new Router();

This part loads the Oak module and creates a new router.

  • GET request handler /json:
router.get("/json", (context) => {
  const jsonResponse = {
    message: "This is a JSON response",
  };
  context.response.body = jsonResponse;
});

This handler will return JSON-formatted responses to GET requests to /json.

  • GET request handler /html
router.get("/html", (context) => {
  const htmlResponse = "<h1>This is an HTML response</h1>";
  context.response.headers.set("Content-Type", "text/html");
  context.response.body = htmlResponse;
});

This handler will return an HTML-formatted response to a GET request to /html. It sets the Content-Type header of the response to text/html.

  • GET request handler /header
router.get("/header", (context) => {
  context.response.headers.set("Custom-Header", "Custom Value");
  context.response.body = { message: "Check the headers!" };
});

This handler sets custom values ​​for response headers on GET requests to /header and returns the message in JSON format.

  • GET request handler /error
router.get("/error", (context) => {
  const errorResponse = {
    error: "An error occurred",
  };
  context.response.status = 500;
  context.response.body = errorResponse;
});

This handler responds to GET requests to /error with an error message and HTTP status code 500.

  • Application creation and router registration
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

This part creates a new Oak application instance and registers the router with the application.

  • Start server
await app.listen({ port: 3000 });

This part applies a router to the application and configures the server to listen on port 3000. After the server starts, you should see the message “Server started on port 3000” on the console.

In this code I am generating different kinds of responses inside the route handler. Features include JSON responses, HTML responses, custom headers, status codes, error responses, and more. You can use Oak’s convenience methods to generate concise and flexible responses.

test

Below is an example curl command to test each endpoint.

## json
curl http://localhost:3000/json
## html
curl http://localhost:3000/html
## header
curl -I http://localhost:3000/header
## error
curl http://localhost:3000/error

summary

By using the context object’s response property, you can:

  • You can set or change response headers.
  • You can set the HTTP status code of the response.
  • You can set the body data of the response. JSON objects, strings, binary data, etc.

You can return data in the form

  • You can set response encoding and compression.

Oak allows you to perform various operations on response generation within your Deno application. Oak’s context object has many properties and methods that allow for a high degree of customization and control.