E
E
exdude2020-11-10 11:23:51
Node.js
exdude, 2020-11-10 11:23:51

Is it possible to run router in a loop?

The question arose of generating routers in accordance with the list coming from the database, since there are about 220 types of subdomains in the list.
To do this, I decided to use the "express-subdomian" framework.
And wrote the following code:

for (let key in cities) {
        let town = ruToLat(key).toLowerCase();
        router.get('/', async (req, res) => {
            res.render('index', {
                title: `Subdomian is: ${key}`,
                ifIndex: true,
                cities,
                key
            })
        });
        router.get('/euro', (req, res) => {
            res.render('euro', {
                title: 'Subdomian is: ${key} ',

                ifEuro: true,
                cities,
                key
            })
        });
        app.use(subdomain(`${town}`, router));
    }


The variable citiescontains an object with the same list.
Where in the future I pass a list with a key to the page to display data on the page.
But when accessing, let's say "moskva.domian.ru", only the data of the first key is rendered on the page. That is, the list contains the key "moskva" but the loop stopped immediately after the first interval.
What could be the problem? Or am I doing something wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twolegs, 2020-11-10
@exdude

express-subdomian solves the problem where different subdomains need different routers.
You only need to use one router. A subdomain can be obtained for example via `req.subdomains`.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question