Answer the question
In order to leave comments, you need to log in
How to pass data between functions in a route?
There is a route in which we define the user's authorization as the first function ( usersController.loadUser ).
app.get('/', usersController.loadUser, function(req, res) {
res.render('index.ejs', { title: 'Главная страница '});
});
exports.loadUser = function(req, res, next) {
users.loadUser(req.session.id, function(err, result) { //Отправляем запрос в БД
if (err) {
return res.send(err);
} else if (result) { //Если вернулись данные из БД проверяем их и разрешаем доступ к маршруту
next();
} else {
//Либо просим пройти авторизацию
res.redirect('/signin');
}
});
}
Answer the question
In order to leave comments, you need to log in
well, you can add a field to req, it goes through the whole chain
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question