dependabot[bot] on master
build(deps-dev): bump jest from… (compare)
app.use(async (ctx, next) => {
arr.push(1);
await next();
arr.push(3);
});
app.use(async (ctx, next) => {
arr.push(2);
await next();
});
app.use(async ctx => {
ctx.body = JSON.stringify(arr);
});
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.
await next();
is called. so anything after the next call is happening on the way back up.
ctx.before = true;
await next();
// Go do other middlewares...
ctx.after = true;
Hi guys, a question for you:
tl;dr: Trying to create a mock context like this:
const ctx = createContext({
method,
headers,
body,
});
throws TypeErrors
https://stackoverflow.com/questions/57864117/koa-create-context-cannot-set-headers-or-body
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);
}
});
};
.end()
but I could be mistaken on that
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.
Thanks.