Should a middleware (provided by a library) that selectively responds to requests always call next()? #1819
-
Hi folks; I maintain a project called PostGraphile which helps build powerful GraphQL APIs backed primarily by a Postgres database. PostGraphile supports many webserver frameworks, including Koa, through adaptors. PostGraphile handles calls to Here's some pseudocode to give you the flavour of what's going on. In PostGraphile we might have: export const postgraphileMiddleware = async (ctx, next) => {
const response = await getPostGraphileResponseFromKoa(ctx);
if (response) {
// PostGraphile has a response for this request; send the response and stop.
const { headers, statusCode, body } = response;
ctx.response.set(headers);
ctx.response.status = statusCode;
ctx.response.body = body;
} else {
// PostGraphile doesn't want to handle this request; pass on to downstream middleware.
return next();
}
}; And in user code: import Koa from 'koa';
import { postgraphileMiddleware } from "postgraphile/servers/koa"; // NOT REAL!
const app = new Koa();
// app.use(compress());
// app.use(logging());
// ...
app.use(postgraphileMiddleware);
// ... From the docs/guide.md file:
My understanding from reading the above is that response middleware can choose whether or not to call However, a user has complained that this isn't the case, and that a middleware provided by a library should always call I'd like to get a project maintainer's input on this, and would be happy to contribute the conclusion back to the response middleware part of the guide.md file. In particular, if Please note that PostGraphile can respond in a large number of ways, including things like a HTTP 204 with no content (i.e. Thanks for your input on this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
your set up looks right since your middleware is making the response if the user wants to overwrite the library, they can create a middleware before calling yours. this is the purpose of app.use(async (ctx) => {
await next()
// do something with the PostGraphile response
})
app.use(postGraphileMiddleware) |
Beta Was this translation helpful? Give feedback.
your set up looks right since your middleware is making the response
if the user wants to overwrite the library, they can create a middleware before calling yours. this is the purpose of
next()