question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware not working when using a router

See original GitHub issue

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 });

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
lengfangbingcommented, May 25, 2020

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 });

you should write middleware before route. you can write

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

// write here  router.get().......
Read more comments on GitHub >

github_iconTop Results From Across the Web

express global middleware not being called - node.js
Thanks very much - this did the trick! My takeaway is that app.router has to be the last global middleware added before defining...
Read more >
Middleware not working - Laracasts
Middleware not working. Around all of my routes I have but I am still able to access all of my routes. Copy Code...
Read more >
Using middleware - Express.js
To skip the rest of the middleware functions from a router middleware stack, call next('route') to pass control to the next route. NOTE:...
Read more >
koa-router middleware not working on route level - Reddit
koa-router middleware not working on route level. hey all, not sure if this is the correct subreddit to be posting to, but neither...
Read more >
Complete Guide to Express Middleware - Reflectoring
We call app.use() to add a middleware function to our Express ... project for running our examples of using middleware functions in Express....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found