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

159
Visualizações
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 Respostas
Responde à pergunta

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 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