P
P
Pavel Tkachenko2018-04-06 17:43:31
Node.js
Pavel Tkachenko, 2018-04-06 17:43:31

How to get a list of all routes and methods in Express?

The essence is that, it is necessary to receive all routes of application and methods of these routes.
For example:
File ./app.js

// Подключаем Express
    var express = require('express');
    var app = express();
// Подключаем файл с маршрутами
    require('./routes') (app);

In the file ./routes/index.js
// Файлы маршрутов
    var login = require('./login.js')
    var users = require('./users.js')
module.exports = function(app) {
    app.use('/users,users);
}

File ./routes/users.js
var express = require('express');
    var router = express.Router();
    
    router.get('/', (req, res) => {
    // .... Страница со списком пользователей
})
    router.get('/add', (req, res) => {
    // ....  Добавление пользователей
})
    router.post('/add', (req, res) => {
    // ....  Добавление пользователей
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Tkachenko, 2018-04-07
@Pavel_Tkachenko

Maybe someone will come in handy. Using module npmjs.com/package/express-list-endpoints
It outputs my routes for example

[ { path: '/login', methods: [ 'GET' ] },
{ path: '/login', methods: [ 'POST' ] },
...]

For correct display, it is better to use or app.routeor as in my case (tweak the code a little)router.route

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question