adonisjs / v6-docs

Documentation website for AdonisJS v6
38 stars 53 forks source link

docs(routing): add information for route splitting #27

Closed emilesabatier closed 4 months ago

emilesabatier commented 4 months ago

Hi, first PR here!

Had some difficulties to find out why splitting my routes into differents files was not working.

Found the answer thanks to Soares on the discord: https://discord.com/channels/423256550564691970/1204948136163020852

One must export a function. It could be useful to be mentionned in the docs, mainspring of this PR.

RomainLanz commented 4 months ago

This is not how you are expected to split your route. Also, note this has nothing to do with AdonisJS and is a more JavaScript-related question.

emilesabatier commented 4 months ago

Thank you, I understand this is not AdonisJS related, so we can close the PR.

But what is the best AdonisJS way to split my routes ?

RomainLanz commented 4 months ago

But what is the best AdonisJS way to split my routes ?

There is no "best" way, but I would recommend creating a dedicated file and declaring your route there, then importing it from your main file.

// start/routes/v1.ts

import router from '@adonisjs/core/services/router'

router.group(() => {
  router.get('/nome', () => 'Adonis')
}).prefix('api/v1')
// start/routes.ts

import './routes/v1.js'

router.group(() => {
  router.get('/nome', () => 'Adonis')
}).prefix('api')