2

I have a simple pages/api/thumbnail.js file with a handler:

export default async function handler(req, res) {
  console.dir(req.params)
  // ...
}

On a pure express server, it is possible to have a "path parameter", but on NextJS I got a error 404 if a try to pass some path on my API.

Example:

http://localhost:3000/api/thumbnail >> OK, it works

Now with a path:

http://localhost:3000/api/thumbnail/something >> ERROR, 404 not found!

Using NextJS API is it possible to use path parameters or only query string?

PS. I am using Vercel, if using a rewrite is the only solution, its acceptable to me.

1 Answer 1

5

You can try adding three dots on the file name like this: pages/api/thumbnail/[[...params]].js Then you can load the params in your handler from req.query

Source: optional-catch-all-api-routes in next.js docs

Sign up to request clarification or add additional context in comments.

2 Comments

How to access query params in this case? i.e. if we have both path and query params?
@darKnight I am not working with next.js for a while, but I used to import useSarchParams, see here nextjs.org/docs/app/api-reference/functions/use-search-params

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.