Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NextJS API error : "TypeError: res.status is not a function"

Context

I’m using NextJS v11.1.1-canary.11, React v17.0.2 and Typescript v4.3.5.

I wanted to create a simple API endpoint, based on NextJS Typescript documentation, so I’ve created a file in pages/proxy folder named login.tsx that is containing very simple API code:

import type { NextApiRequest, NextApiResponse } from 'next'

export default function Login(req: NextApiRequest, res: NextApiResponse) {
  res.status(200).json({ name: 'John Doe' })
}

My problem

When I try to call that API endpoint, an error occurs :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: res.status is not a function
    at Login (C:\wamp64\www\project-front\.next\server\pages\proxy\login.js:19:7)

Full stacktrace here

I’ve done researches and I can’t find any case like me on Google or Stackoverflow, I’m new in NextJS so I’m probably misunderstanding something. Does someone have an idea of what I’m doing wrong ?

>Solution :

Your example works fine.

You can solve this issue by placing your login.ts file at pages/api/{whatever_you_want}

Next docs:

Any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a page. They are server-side only bundles and won’t increase your client-side bundle size.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading