#Res
1 messages · Page 1 of 1 (latest)
The error message you're seeing, TypeError: res.status is not a function, typically indicates that the res object you're trying to use does not have a status method. This could be due to a few reasons:
- You might not be using the
resobject provided by Next.js in your API route handler. In Next.js API routes, theresobject is an instance of the Server Response object (https://nodejs.org/api/http.html#http_class_http_serverresponse), which includes a set of Express.js-like helper methods, includingres.status(code). Here's an example of how it's used:
export default function handler(req, res) {
res.status(200).json({ name: 'John Doe' })
}
(Source: Next.js API Routes (https://nextjs.org/docs/pages/building-your-application/routing/api-routes))
- You might be trying to use
res.statusin a context whereresis not defined or is a different object. Make sure you're usingres.statusinside an API route handler, and thatresis the second argument to your handler function.
If you're still having trouble, please provide more context or a snippet of your code, and I'll do my best to help you debug the issue.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@hushed valley kapa.ai is still learning and improving, please let us know how it did by reacting below
@brisk jolt app router redirect
Let me check the knowledge base for you. One moment! :books:
In Next.js, you can handle redirects in several ways:
- Using
next.config.jsfor static redirects: You can define redirects in yournext.config.jsfile using theredirectskey. This function should return an array of objects, each withsource,destination, andpermanentproperties.
module.exports = {
async redirects() {
return [
{
source: '/about',
destination: '/',
permanent: true,
},
]
},
}
In this example, requests to /about will be permanently redirected to /. The permanent property, when set to true, uses the 308 status code, instructing clients/search engines to cache the redirect forever. If false, it uses the 307 status code, which is temporary and not cached. (Source: Next.js Redirects (https://nextjs.org/docs/app/api-reference/next-config-js/redirects))
- Using API Routes for dynamic redirects: You can also perform redirects using an API Route. This method is recommended if you need to dynamically query an external resource to fetch your list of redirects.
export default (req, res) => {
if (req.url.startsWith('/example')) {
res.statusCode = 308
res.setHeader('location', '/test')
return res.end()
}
res.statusCode = 404
return res.end('Not found')
}
In this example, requests starting with /example will be redirected to /test. (Source: Vercel Guide (https://vercel.com/guides/how-can-i-increase-the-limit-of-redirects-or-use-dynamic-redirects-on-vercel))
- Using Middleware for redirects: Middleware allows you to run code before a request is completed. You can modify the response by redirecting based on the incoming request.
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
return NextResponse.redirect(new URL('/home', request.url))
}
In this example, all requests will be redirected to /home. (Source: Next.js Middleware (https://nextjs.org/docs/app/building-your-application/routing/middleware))