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

213
Vistas
How to handle Express errors without relying on the function's length property?

From Express's error handling docs:

Define error-handling middleware functions in the same way as other middleware functions, except error-handling functions have four arguments instead of three: (err, req, res, next). For example:

app.use(function (err, req, res, next) {
  console.error(err.stack)
  res.status(500).send('Something broke!')
})

It seems that Express's .use(middlware) inspects the middleware function's length to see how many arguments it takes, and if four, then it treats it differently to other middleware functions, passing it an error as the first argument.

This is incompatible with modern linting setups like XO or Airbnb, as it forces you to define an extra trailing parameter tha doesn't get consumed (i.e. next in the above example). Also, unused trailing parameters might get removed by some automated code transformations, which is just something I don't want to worry about. In my opinion, Function.prototype.length is best kept for metaprogramming/introspection as opposed to affecting real production behaviour.

Is there any other, more explicit way to define an error handler in Express, that doesn't force you to imply intended functionality through the number of parameters your function takes?

about 4 years ago · Santiago Trujillo
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