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

155
Views
Why does the server run both of these handlers in node?

I have my node app set like this- I've got two handlers, one for requests to the root URL and the other for the slug, and when I go to the root, it does load up, then crashes. When I go to the slug URL, same thing. This error shows up:

url is pointing to root
url has a slug
throw err; // Rethrow non-MySQL errors

What did I do wrong?


app.get(
    '/:slug',
    (req, res) => {
        connection.query(`select * from blogs where slug="${slug}"`,
            (error, results) => {
                if (error) console.log(error);
                console.log('url has a slug');
                res.render(
                    'somepage',
                     results
                )
            }
        )

    }
)

app.get(
    '/',
    (req, res) => {
        connection.query(`select * from blogs`,
            (error, results) => {
                if (error) console.log(error);
                console.log('url is pointing to root');
                res.render(
                    'index',
                    {
                        title: 'Home page',
                        blogs: results
                    }
                )
            }
        )

    }
)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

One possible explanation for the error you observe:

After loading the home page with GET /, browsers make an additional GET /favicon.ico request to retrieve the icon that decorates the browser tab (which is on StackOverflow). That additional request would be served by your "slug route", with req.params.slug = "favicon.ico". I expect the variable slug in your code to be undefined. Anyway, the select statement will probably fail during this additional request.

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!