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

Let's test it...

Our configurable middleware function is not useful on its own. Create a simple ExpressJS application to see our middleware actually working:

  1. Create a new file named configurable-middleware-test.js
  2. Include our middleware-logger.js module and initialize a new ExpressJS application:
       const express = require('express') 
       const loggerMiddleware = require('./middleware-logger') 
       const app = express() 
  1. Use the use method to include our configurable middleware function. When the enable property is set to true, your logger will work and log every request's status code and URL to the terminal:
      app.use(loggerMiddleware({ 
         enable: true, 
      })) 
  1. Listen on port 1337 for new connections:
      app.listen( 
           1337, 
           () => console.log('Web Server running on port 1337'), 
         ) 
  1. Save the file
  2. Open a terminal and run:
      node middleware-logger-test.js
  1. In your browser, navigate to:
      http://localhost:1337/hello?world
  1. The Terminal should display:
      Status Code: 200 URL: /hello?world