Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda