Rodrigo
Rodrigo

Reputation: 291

Using path parameter on API of NextJS

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.

Upvotes: 2

Views: 9692

Answers (1)

Zavael
Zavael

Reputation: 2468

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

Upvotes: 5

Related Questions