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.

Filtered middleware never run

See original GitHub issue

Hi there,

I’m trying to replace Express with Polka in a small project, and I’m facing an issue where a “filtered” middleware is never run :

const polka = require('polka');
const serveStatic = require('serve-static');

/*
 *
 */

const app = polka();

// Always run, as expected
app.use((req, res, next) => {
  console.log(req.method, req.url);
  next();
});

// Never run, but I expect it to be when navigating to /admin/**/*
app.use('/admin', (req, res, next) => {
  console.log('  @admin!');
  next();
});

// Always run, as expected
app.use(serveStatic(__dirname + '/_static'));

/*
 *
 */

app.listen(4444, () => console.log('Listening on port 4444...'));

It’s probably a very silly error but I don’t see what I’m doing wrong here.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
flawytecommented, Mar 12, 2019

Ok. Thank you!

1reaction
lukeedcommented, Mar 12, 2019

I would fix it by moving serve-static to its own path. Most of the time, you don’t need it running globally (it’s slow and induces a significant performance hit too).

Maybe something like this:

polka()
  .use('/assets' serveStatic)
  .use('/admin', ...)

Polkadot doesn’t have a router or middleware runner built in, so you would/could arrange it however you’d like! It’s not affected by anything in Polka, no 😃

There is a polka@next release out btw. It has the ordering changes so it might feel more comfortable/familiar to you

Read more comments on GitHub >

github_iconTop Results From Across the Web

couldn't filter request through middleware - Stack Overflow
I am trying to filter my http request by using middleware. I want to check if the "friends_id" that i am getting from...
Read more >
Is it possible to have an EF Core middleware or something that can ...
I'm trying to apply a particular filter logic to my queries based on some parameters that will be different for every user. So,...
Read more >
Conditional Middleware based on request in ASP.NET Core
This post looks at how to configure ASP.NET Core middleware in a way that allows you to have different middleware for different types...
Read more >
Defining middleware | LoopBack Documentation
initial - The first point at which middleware can run. ... the response chain, the “catch-all” middleware is never triggered when a get...
Read more >
How to Write Middleware for Express.js Apps - Stormpath
In other frameworks “middleware” is called “filters”, ... As you write your own middleware you will run into some pitfalls, but fear not!...
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