P
P
Pavel Tkachenko2018-05-04 23:37:12
Express.js
Pavel Tkachenko, 2018-05-04 23:37:12

How to get options value via app.use?

The bottom line is that at startup, the application writes, updates or deletes obsolete routes in the database. Information such as route name, description, etc. can be changed in order to call a middleware when going along this route, which will load information from the database and display it, for example, in res.locals.title, etc. There are no problems with routes without parameters, but here's how to deal with routes with parameters, for example '/:id'
Implementation example:

app.use(function(req, res, next) {
  // Поиск в бд информацию о маршруте
  Routes.findOne({'path' : req.url}, function(err, rout) {
    /* Пока временное решение поскольку путь генерируемый '/admin/routes/5aeb87729c92911740622fc5',
    в зарегистрированных есть '/admin/routes/:id' */
    if (rout == null) {
        res.locals.title = req.url
    }
   // Иначе отправляем данные из бд
    else {
        res.locals.title = rout.title
    }
    next()
  })
})

Can someone tell me something, is there another way to implement this functionality?

Answer the question

In order to leave comments, you need to log in

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

Maybe this is a crutch-bicycle method, but I haven’t come up with another yet, and it only works if the parameters are not more than 1, well, for starters, and then go ahead, it will still be drunk.
Here is my implementation:

app.use(function (req, res, next) {
            Routes.findOne({'path' : req.originalUrl}, function(err, pageInfo) {
                if(err) {
                    res.locals.title = 'Ошибка чтения информации о странице из БД';
                    res.locals.description = err;
                    console.log(err); 
                    next();
                }
                if(pageInfo == null) {
                    var search = req.originalUrl.split('/');
                    search.splice(-1,1,':');
                    search = search.join('/');
                    Routes.findOne({'path' : {$regex: search}}, function(err, pageInfo){
                        if(err) {
                            res.locals.title = 'Ошибка чтения информации о странице из БД';
                            res.locals.description = err;
                            console.log(err); 
                            next();
                        }
                        if(pageInfo == null) {
                            res.locals.title = 'Допели скрипт скотина';
                            res.locals.description = err;
                            console.log(err); 
                            next();
                        }
                        else {
                            res.locals.title = pageInfo.title;
                            res.locals.description = pageInfo.description;
                            next()
                        }
                    })
                }
                else {
                    res.locals.title = pageInfo.title;
                    res.locals.description = pageInfo.description;
                    next()
                }
            })
        });

MB someone will have additions, I will only be glad

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question