How to do it...

What your configurable middleware function will do is simple: it will print the status code and the URL when a request is made.

  1. Create a new file named middleware-logger.js
  1. Export a function that accepts an object as the first argument. The function expects the object to have a property enable, which can be either true or false:
      const logger = (options) => (request, response, next) => { 
          if (typeof options === 'object' 
              && options !== null 
              && options.enable) { 
              console.log( 
                  'Status Code:', response.statusCode, 
                  'URL:', request.originalUrl, 
              ) 
          } 
          next() 
      } 
      module.exports = logger 
  1. Save the file
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.227.74