In the middleton of the middleware

@glrodasz

Guillermo Rodas

Engineer at Auth0

Organizer of Medellín CSS, CSS Conf CO

and​ Medellín Identidad y Seguridad

@glrodasz

GDE Web Technologies

Curso de Autenticación con OAuth

Curso de Express.js

¿Qué es Express?

Web apps, web apis, web services.

Open Source, MIT License.

TJ Holowaychuk,

Sinatra (Ruby).

Icons from www.flaticon.com is licensed by CC 3.0 BY, slide from Curso avanzado de Express.js (Platzi, 2018)

Icons from www.flaticon.com is licensed by CC 3.0 BY, slide from Curso avanzado de Express.js (Platzi, 2018)

Junio 2014

Septiembre 2015

Enero 2016

Slide from Curso avanzado de Express.js (Platzi, 2018)

Caracteristicas

  • Minimalista
  • Template engines
  • Routing
  • Middlewares
  • Plugins (as middlewares)

Demo

¿Qué es un middleware?

middleware

/ˈmɪdlwɛː/

software that acts as a bridge between an operating system or database and applications, especially on a network.

Slide from Curso avanzado de Express.js (Platzi, 2018)

Slide from Curso avanzado de Express.js (Platzi, 2018)

Tipos de middleware

  • 3rd party
  • Router level
  • Application level
  • Built-in
  • Error-handling

Demo

¿Qué es Joi y Boom?

Object Schema Validation

HTTP-friendly error objects

Slide from Curso avanzado de Express.js (Platzi, 2018)

Manejo de errores usando un middleware

Slide from Curso avanzado de Express.js (Platzi, 2018)

  • Errors synchronous inside route handlers or middleware, require no extra work.
    • throw new Error("Oops!");

 

  • Errors asynchronous inside route handlers or middleware, you must pass them to the next() function.

    • if (err) {  next(err); } // Pass errors to Express.

 

  • If you pass anything to the next() (except the string 'route'), Express regards the current request as an error and will skip any remaining non-error handlers and middleware.

    • fs.writeFile("/inaccessible-path", "data", next);

 

  • Must catch errors asynchronous inside route handlers or middleware, and pass them to Express for processing.

    • setTimeout(function () { try { throw new Error("Oops!"); } catch (err) { next(err); } }, 100);

  • The stack trace is not included in the production environment.

  • You define error-handling middleware last, after other app.use() and routes calls;

  • Default error handler: Call next() with an error after you have started writing the response (Streams) the default error handler closes the connection and fails the request.

    • If (res.headersSent) { return next(err) }

  • Default error handler can get triggered if you call next() with an error in your code more than once, even if custom error handling middleware is in place.

  • if you have a route handler with multiple callback functions you can use the "route" parameter to skip to the next route handler.

Demo

Validación de datos usando un middleware

Slide from Curso avanzado de Express.js (Platzi, 2018)

Demo

Middlewares populares

  • body-parser

  • cors

  • morgan

  • helmet

  • express-debug

  • express-slash

  • passport

Gracias, preguntas?

In the middle of the middleware

By Guillermo Rodas

In the middle of the middleware

Explaining how middlewares work in Express.js

  • 524