Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 574
cbdeveloper Avatar asked Sep 06 '25 01:09

cbdeveloper


2 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();
like image 158
Optimus Prime Time Avatar answered Sep 07 '25 19:09

Optimus Prime Time


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");
like image 43
cbdeveloper Avatar answered Sep 07 '25 20:09

cbdeveloper