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

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

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