How do I set the default return status to 200?

1.5k Views Asked by At

When handling a request that does not need a response I would like the default status code to default to 200 (at the moment it is 404).
I have a bunch of api endpoints that insert in to a DB. At the moment I have to set ctx.status OR ctx.body to return a 200 (if left unset then it return a 404). Is there a way to return 200 by default?
Thanks

1

There are 1 best solutions below

0
On

You can create middleware like that

async function setDefaultResponse (ctx, next) {
    await next();

    if (!ctx.body) {
        ctx.body = {};
    }
};

And include this one before routers

app.use(setDefaultResponse);
app.use(router)