Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
  • Jul 02 18:21
    titanism commented #1645
  • Jul 02 18:13
    3imed-jaberi commented #1645
  • Jul 02 09:57
    miwnwski closed #1404
  • Jul 02 09:57
    miwnwski commented #1404
  • Jul 02 09:42
    miwnwski labeled #1008
  • Jul 02 09:40
    miwnwski commented #1053
  • Jul 02 09:37
    miwnwski closed #707
  • Jul 02 09:37
    miwnwski commented #707
  • Jul 02 09:37
    miwnwski closed #694
  • Jul 02 09:37
    miwnwski commented #694
  • Jul 02 09:22
    miwnwski labeled #1646
  • Jul 02 09:22
    miwnwski commented #1646
  • Jul 02 09:20
    miwnwski commented #1455
  • Jul 02 09:15
    miwnwski closed #1525
  • Jul 02 09:15
    miwnwski commented #1525
  • Jul 02 09:14
    miwnwski commented #1561
  • Jul 02 09:11
    miwnwski labeled #1420
  • Jul 02 09:03
    miwnwski closed #1620
  • Jul 02 09:03
    miwnwski commented #1620
  • Jul 02 08:50
    miwnwski closed #1653
Mike Hamilton
ah, so it goes down, does the response, then goes up
Casey Britt

Anyone able to help me better understand a Middleware pattern? I'm wondering if there is an article or tutorial out there about how to add Middlewares to your own code rather than writing Middlewares for existing packages. Or even just one that breaksdown how Middlewares work in something like Koa. Searching google just gets me info on how to write better middelwares for things like Koa/Express.

I'm particularly interested in the pattern Koa uses with context and would love an article that pulls the general flow out into some easier to comprehend code as jumping through the many koa packages is difficult for me to follow.

@yourfavorite i would start with https://koajs.com where they give some simple example of middleware in koa
the docs there are pretty simple and that’s simply because koa is pretty simple in what it does.
the one thing to note is that koa calls middleware down and then back up when a await next(); is called. so anything after the next call is happening on the way back up.
Casey Britt
@jamemackson I don't quite follow the way down and way back up. Are you saying that Koa goes mw1 -> next() -> mw2 -> await next() -> mw3 -> next() -> mw2 -> ??
yes. next passes control down the middleware stack until something sets a response and the flow goes back up executing any code that follows the next call. see the example above that mike posted a day or two ago.
Casey Britt
Oh I see
So basically
ctx.before = true;
await next();
// Go do other middlewares...
ctx.after = true;
yup. that’s about it.
Casey Britt
What is a practical reason for wanting to do this?
there are times you want to do something with a response going out. maybe time a response or validate some data
Jackie Gleason
Can someone help me with koa-jwt. I have it configured but for some reason it is still throwing a 401 even after I am signed in.
Anyone know how i can modify a specific visitor sessions outside the scope of koa (ctx) ? Getting certain info from Jayson TCP sockets that i want to apply to the correct users session.
Kirk Gordon
Does Koa support keep-alive by default? If not, how can one enable it?
Here is a new project. https://github.com/goa-go/goa just like koa for golang, I'm looking for someone to join us.
Eyal Perry
@NicholasCao I love the name :D and I do love koa. Unfortunately I do not know go at all
It doesn't matter, thank you for your start
Wow no chat since 1 month...
Can I add an extra arguments to session.save with koa-session?
I want to add some data to the stored session in the same insert
Vamsi Ampolu

Hi guys, a question for you:

tl;dr: Trying to create a mock context like this:

const ctx = createContext({

throws TypeErrors


@vamsiampolu I think headers, methods don’t have setting when an hTTP method is get other than post. So context are executed once upon start of the server. perhaps the app.use( ) where you intialize context to run should be higher in the code chain prior to other middleware?
Vimal Mistry
Hello everyone. I want to use Koa for my next project. What is actual real world advantage of koa over express?. I want it for my api backend.
Vimal Mistry
Screenshot 2019-09-19 at 11.59.28 PM.png
AuthorizationMiddleware is still calling on first routes why?
@vimalmistry are you having an issue or are you asking why auth is being called in your load routes?
Vimal Mistry
@johndeguerto Thanks for reply. I have solved this issue by calling authRoute.use(myMiddleware) :)
Hi, what is best npm for koa/health
Hi, having a problem with setting ctx.body = a stream (sort of). Im using csv-stringify npm module's streams support, so my code is generally doing something like this: ctx.body = stream; await someCodeThatGeneratesCsvRows(stream); stream.end();
I should first say that the code works. The thing that's confusing me is that no rows come to the browser until all rows are generated (e.g. until stream.end() is called). I changed my stream writer to be something a bit more complex, like this, which vaguely what they say to do anyway in the Writable stream docs, const writer = (stream) => (obj) => { return new Promise((resolve) => { if (!stream.write(obj)) { stream.once('drain', resolve); } else { process.nextTick(resolve); } }); };
and when I do that, stream.write does indeed return false at some point but the drain event never arrives, so my request just hangs.
my vague guess is that Im doing something wrong and Koa isn't reading from the stream until .end() but I could be mistaken on that
ok well I figure out how to get it working...after reading some koa source code. I need to not await someCodeThatGeneratesCsvRows(stream) and instead do ctx.body = stream; someCodeThatReturnsAPromiseThatIWontAwait(stream).then(() => stream.end());
...because Koa doesn't attach the pipe to the stream until after my middleware returns. Is this the way I'm supposed to be doing this? I don't think there's any examples that make this particularly clear in the docs...
If its not clear why I might be doing this, Imagine I'm reading blocks of data from a db (or another api) to fetch parts of a dataset, and then tranforming that data into CSV, and writing the CSV rows to a stream. So the method that generates the dataset is async, but I want koa to stream the results (obviously) as they arrive. And so given that Koa is built on top of async await it seemed natural to me that I could assign the stream and then pump data into the stream via awaiting as necessary...
Jonas Amundsen
if I append a middleware in the runtime of another middleware (IE. during a request), will it be considered and possibly invoked for said request?
Guy Korland
Is there a way to load part of a session or lazy load part of it? And is there a way to store only part of the session and the all big session when only a small part of it changed?
Rajiv Mothilal
Hi everyone, I'm a newbie to the koa framework. I need some assistance with an issue. I need to access the incoming request headers before request authentication is done. I need to insert additional headers to be used in subsequent functions/processes. Any help would be greatly appreciated. Thanks Rajiv
Shashank Agarwal
Hi, a NodeJS noob here,

I intend to create an application and reuse some app logic by forwarding an API call from one route to another.

for example. A request is made for /subscribed-users and i want to add to this context and call the /users route internally.

How can i achieve this with KOA?
Any help would be welcome.


Mayursinh Sarvaiya
@shashank1010 , ctx.redirect should be used for this purpose. Read the documentation here https://github.com/koajs/koa/blob/master/docs/api/response.md#responseredirecturl-alt.
hey i wanted to share my article https://medium.com/@art_deco/how-i-compiled-koa-with-closure-63c064d7fa36 maybe someone can find it useful
Hi! What does the name koa mean?
I'm guessing it comes from Co-routines
Mr Swedish god @jeffijoe, are you alive?
Jeff Hansen
@fjeddy I'm danish lol