dewijones92
dewijones92

Reputation: 1359

How do I set the default return status to 200?

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

Upvotes: 0

Views: 1690

Answers (1)

saaaaaaaaasha
saaaaaaaaasha

Reputation: 393

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)

Upvotes: 1

Related Questions