Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
Authenticating static paths, nodejs express passport html

I am relatively new to web development and I am trying to setup a basic authentication. It works fine for non-static paths, however I am having trouble understanding how static path works. I recently discovered that when I request my authenticated page (/home) using a static path .../home.html, it serves the path no matter what. I already tried to authenticate it the same way I authenticated /home, however it does not work, although I see from console log that authentication fails, so isAuthenticated is called.

I would greatly appreciate if someone can point me to the right direction, as I am trying to understand the difference between asking /home, and /home.html in order to have a proper authentication. I also noticed that most websites I visit online does not have any static paths when I navigate through them. Since only way I know how to redirect a page from button is to use href=/path.html, I also serve static paths after the first button click. It would be good to know how to serve a non-static path (href=/path do not work if I dont add .html at the end)

Probably answer is rather straightforward, but I do not know which words I should look into and to my best knowledge I couldn't find this on stackoverflow as well. So please help me with this noob question.

Here is where I set the middlewares:

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static('public'));
app.set('views', __dirname + '/public');
app.engine('html', engines.mustache);
app.set('view engine', 'html');

app.use(require('express-session')({
    secret: 'so secret much safety',
    resave: true,
    saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());

Here is where I try to authenticate:

function isAuthenticated(request, response, next) {
    console.log('isauthenticated:'+request.isAuthenticated());
    if (request.isAuthenticated()){return next();}
    else{response.redirect('/');}
}


app.use('/home', isAuthenticated, function(request, response){
    response.render('home.html');
    //response.send('if you are viewing this page it means you are logged in');
});

app.use(express.static(__dirname + '/home'), isAuthenticated, function(request, response){
    //should serve the page here
});
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

When i tried moving views away from public folder the nodejs were unable to find them although, I changed view directory. Then I discovered changing to router from app fixed the problem completely. This was probably quite trivial but it took me some time to figure my way around. If another amateur has the same problem and ends up here, following worked for me:

var router       = express.Router();
...
app.set('views', __dirname + '/views');
...
router.use('/home', isAuthenticated, function(request, response){
    response.render('home');
});

router.use(express.static(__dirname + '/home'), isAuthenticated, function(request, response){
    console.log('it works');
});
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda