Alexander Suraphel
Alexander Suraphel

Reputation: 10613

Nested route gives 404 not found on a fastify app with file based routing

I've created an app with fastify generate but nested routes are giving me 404. index.ts works a directory but anything else I add is not found. I have /a/b/index.ts which works but /a/b/c.ts which doesn't. But if i move c.ts to the parent dir, it's detected. And i have fastify.get('/c'... on c.ts. How can I fix this?

Upvotes: 1

Views: 997

Answers (1)

Manuel Spigolon
Manuel Spigolon

Reputation: 12900

Fastify is about security, so you need to check the plugins' documentation you are using under the hood.

In your case, you must change the app.ts file like so:

  void fastify.register(AutoLoad, {
    dir: join(__dirname, 'routes'),
    maxDepth: 4, // default is 2
    options: opts
  })

by doing this, fastify-autoload will search for nested folders deep to 4 levels.

Then you will be able to call: http://127.0.0.1:3000/a/b/c/c assuming you created src/routes/a/b/c/c.ts

Upvotes: 2

Related Questions