profile
viewpoint

Ask questionsMiddleware not working when using a router

I placed middleware after a router and it never gets called for some reason. I want to log the status of a response. Below is the code used.

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

const app = new Application();

const router = new Router();
router.get("/", async (context, next) => {
  context.response.body = "hello world";
  await next();
});

app.use(router.routes());
app.use(router.allowedMethods());

app.use(async (context, next) => {
  console.log(context.response.toServerResponse().status);
  await next();
});

await app.listen({ port: 8000 });
oakserver/oak

Answer questions kitsonk

This is fixed in master now. Getting response.status will return what should be sent in the response if it is not explicitly set. There is also improvements so that calling .toServerResponse() doesn't "break" the response (though you shouldn't need to do it in most cases).

useful!

Related questions

No questions were found.
Github User Rank List