user4855927
user4855927

Reputation:

How to list all paths in an Express application using Node.js, like "rake routes" command in Rails Application?

I've attempted to install several NPM packages into my express app that allow me to view my apps routes but I haven't had any success. I'm used to using rake routes to display my routes in my rails applications but haven't found anything similar in express. What are the best ways to get a similar functionality in express?

Upvotes: 1

Views: 6593

Answers (4)

ansh sachdeva
ansh sachdeva

Reputation: 1179

i created this code by reading above answers. this will not only return direct routes, but also routes created via router on specific path:

const app = express()

let allRouters=[
    ["/api/v1/auth",AuthRouter],
    ["/api/v1/user",UserRouter]
]

app
    .use(express.json())
    .use(cookieParser())

allRouters.forEach(it=>app.use(it[0],it[1]))
app.listen(3000, () => console.log("server started at port 3000"))

app.get("/", (req, res) =>{
    let routes = [
        ["/",app._router],
        ...allRouters
    ]
    let routesJs = {}
    routes.forEach(it=>{
        let key = it[0]
        let value  = it[1].stack.filter(r => r.route).map(r => ({method: Object.keys(r.route.methods)[0].toUpperCase(), path: r.route.path}));
        routesJs[key]=value
    })
       res.json(routesJs)
} )

the response for localhost:3000/ looked something like this:

{
    "/": [
        { "method": "GET","path": "/" },
        { "method": "GET","path": "/mail"}
    ],
    "/api/v1/auth": [
        {"method": "POST","path": "/signup"},
        {"method": "POST","path": "/login"},
        {"method": "POST","path": "/securelogin"},
        {"method": "PATCH","path": "/forgotpassword"},
        {"method": "PATCH","path": "/resetpassword"}
    ],
    "/api/v1/user": [
        {"method": "PATCH","path": "/updateprofile"},
        {"method": "GET","path": "/allusers"}
    ]
}

Upvotes: 0

Pablo Merener
Pablo Merener

Reputation: 231

You can run,

npm install express-route-list-cli

and then

npx route-list <relative app file path>

Upvotes: 1

Praveen George
Praveen George

Reputation: 9725

You can find another stack overflow question with same context of listing all paths/routes in an Express application in the given bellow post:

How to get all registered routes in Express?

For a reference you can also visit the following thread regarding "List All REST Endpoints built using Express or Restify"

Once if you successfully follow the last given link you will get the out put path/rout listed like bellow in the image:

enter image description here

Upvotes: 0

cl2205
cl2205

Reputation: 21

Have you looked into npm package pathfinder-ui? I'm not a Rails user, but I believe it's similar to rake routes in the sense that it'll display all registered routes in an Express (4.x) app. It's also distinct in that:

  • it's a GUI that lets you view all your routes as either 1.) a table that can be filtered/searched, or 2.) an expandable tree diagram
  • lets you test your routes with a request builder - an alternative to curl (kind of like using POSTMAN if you've ever worked with it).

It might be offering more than what you're looking for, but it should definitely display your Express routes in a dev-friendly manner and is pretty easy to configure.

I also just came across this package that might be worth a look as well: express-ls-routes.

Upvotes: 2

Related Questions