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.

Root _middleware is called for every request

See original GitHub issue

I’m working on a session middleware which is called in the routes/_middleware.ts to be able to access the session across the app.

As I see it, the root _middleware is called for every request even the one outside the routes (e.g. static resource request, _frsh/alive, etc.) which could be the intended behavior as it’s consider a global middleware.

My issue is that with this behavior, it’s impossible to keep a session initialization in the root middleware and implement features like session flash as the multiple request expires the flashed data.

It could be possible to filter out from the request, but seems pretty unreliable.

Is it the expected behavior ? If so, maybe routes/_middleware could be reserved only for request routed to a route, and we could find another way to implement global middlewares ?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
xstevenyungcommented, Jul 30, 2022

Sounds like a good idea, I will see if I can make a PR for this 👍

1reaction
xstevenyungcommented, Jul 27, 2022

Thanks for the idea @jcs224 , it seems like a nice solution for the time being.

I took some inspiration from your library to make a first draft of fresh-session but must have missed how you handle flash messages, love the work that you’ve done there.

Thanks again for taking the time to point it out, really appreciate it 🙌

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using middleware - Express.js
Middleware functions are functions that have access to the request object ( req ), the response object ( res ), and the next...
Read more >
Complete Guide to Express Middleware - Reflectoring
Middleware in Express are functions that come into play after the server receives the request and before the response is sent to the...
Read more >
Use specific middleware in Express for all paths except a ...
I would add checkUser middleware to all my paths, except homepage. app.get('/', routes.index); app.get('/account', checkUser, routes.account);.
Read more >
How Node JS middleware Works? - Selvaganesh - Medium
A middleware is basically a function that will the receive the Request and Response objects, just like your route Handlers do. As a...
Read more >
Build and Understand Express Middleware through Examples
In Express, you can set up middleware to be “global” middleware; meaning it will be called for every incoming request. Change the contents...
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