How route list from project?

How list all routes in adonis 4.x? how in 3 ./ace route list?

thanks for your help

try this:
adonis route:list

1 Like

it works, thanks

can adonis return this route list in array of json format ?

Nope, it cannot. But I believe, you can access them anyways using the Route class directly.

const Route = use('Route')

const list = Route.list().map((route) => {
  return route.toJSON()
})

console.log(JSON.stringify(list, null, 2))
2 Likes

@virk This would work only inside the route.js file.If i am trying to use it in some other custom.js the Route list is coming out as a empty array.I need to use the same for a jakefile.
Also ‘use’ is not defined error is thrown, but if tried with require would give a empty array.

I am not sure where your custom.js file exactly is and how it is loaded. There is no way to get the routes list without booting the framework.

It will be nice, if you want explain the exact purpose and what exactly you have tried so far

@virk - I m trying to generate a script that should run each time new deploys are pushed, for the same i have created a jakefile, that defines a task to seed the permissions model(for the authorization purpose) in the db.
The jakefile is located at the root folder itself, I have tried executing the shell script inside the jakefile and it executed and list down all the routes, but i need the same in json format so that i can iterate through each route and make entries to the permission model.
Now the problem that i am facing is like in laravel
if i execute “php artisan route:list --json” it would return a json response.
All i am looking is to get all the routes in json format inside a .js file.
i hope it ellaborates the issues that i am facing.

Simply, create your own command (example: route:json) that prints the JSON to stdout or writes to a JSON file and then you can invoke it like any other command.

node ace route:json
2 Likes