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

185
Visualizações
Express.js match only exactly '/' route

So I'm serving a web page on the root route '/', and this page had an authentication middleware. Using regular

app.use('/', authorizeFront, express.static('../client/dist'));

would cause every route to be authenticated, which is what I'm trying to avoid. I've also tried using regex to match exactly '/' but it doesn't seem to be working.

app.use('/^/$/', authorizeFront, express.static('../client/dist'));

Is there any official way to do this? Thanks!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

app.use does a partial match. Use app.get instead.

about 4 years ago · Juan Pablo Isaza Relatório

0

When using app.use("/") this will match any path and method that starts with "/", This happens because app.use() is intended for global middlewares.

Instead you can use app.get("/", yourTargetedMiddlewaer) to target a specific route and a specific method (GET) in this case.

about 4 years ago · Juan Pablo Isaza Relatório

0

Another way to do this could be:


app.use("*", (req, res, next) => {
    if (req.baseUrl === "") { // For / requests baseUrl will be empty
        // Call authenticator and then call next() if auth succeeds else call next(err)
    } else {
        console.info("Bypassing Authentication");
        next();
    }
});

This will hit the middleware for all requests, but you have the control for which request you want to call the authenticator.

about 4 years ago · Juan Pablo Isaza 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