Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!