What is the equivalent of express' res.sendStatus(200) on Next.js api serverless functions?

Viewed 994

I was used to do the following in some of my express routes.

return res.sendStatus(200);

But the res object from NextApiHandlerType does not allow that method.

What would be the equivalent, in this case?

import { NextApiHandler } from "next";

const handler: NextApiHandler = async (req, res) => {
  // DO STUFF
  return res.???   // WHAT SHOULD I PUT HERE TO RETURN THE status CODE WITH THE STANDARD CODE MSG ?
};

I'm currently doing this, but it seems redundant.

return res.status(200).send("Ok");

From: http://expressjs.com/en/api.html

enter image description here

3 Answers

To anyone seeing this now, I was wondering the same thing. The docs aren't 100% clear IMO. This sends a status code and closes out the request.

res.status(200).end();

Here is what you can find in the documentation :

https://nextjs.org/docs/api-routes/response-helpers

The response (res) includes a set of Express.js-like methods to improve the developer experience and increase the speed of creating new API endpoints, take a look at the following example:

export default function handler(req, res) {
  res.status(200).json({ name: 'Next.js' })
}

AFAIK, this is currently not possible inside a Next.js serverless function. I'm using "next": "10.1.3".

The closest alternative is, indeed:

return res.status(200).send("Ok");
Related