Функция next() (простой вопрос)
Почему не работает переход по функции next()?
Браузер постоянно делает запросы, страница загружается, в терминале Node.js вижу 'checkAuth() is TRUE'. Но переход по адресу не происходит.
function checkAuth() {
return app.use((req, res, next) => {
if (req.user) {
console.log('checkAuth() is TRUE');
next();
} else {
console.log('checkAuth() is FALSE');
res.redirect('/');
}
});
}
app.get('/home', checkAuth(), (req, res) => {
res.sendFile(__dirname + "/private/home.html")
});
А вот такая конструкция работает:
function checkAuth() {
return app.use((req, res, next) => {
if (req.user) {
console.log('checkAuth() is TRUE');
//next();
res.sendFile(__dirname + "/private/home.html");
} else {
console.log('checkAuth() is FALSE');
res.redirect('/');
}
});
}
app.get('/home', checkAuth(), (req, res) => {
//res.sendFile(__dirname + "/private/home.html")
});
|
function checkAuth(req, res, next) {
if (req.user) {
console.log('checkAuth() is TRUE');
next();
} else {
console.log('checkAuth() is FALSE');
res.redirect('/');
}
}
app.get('/home', checkAuth, (req, res) => {
res.sendFile(__dirname + "/private/home.html")
});
app.use(...) - повесить глобальный middleware app.get('/', () => здесь router specific middleware ) |
Цитата:
|
| Часовой пояс GMT +3, время: 07:20. |