MERN Quick Start Guide
上QQ阅读APP看书,第一时间看更新

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