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

How to do it...

Let's build a working example. We will include the morgan configurable middleware function with the dev format to display information of every request.

  1. Create a new file named morgan-logger.js
  2. Initialize a new ExpressJS application:
      const express = require('express') 
      const morgan = require('morgan') 
      const app = express() 
  1. Include the morgan configurable middleware. Pass 'dev' as the format we will use as the first argument to the middleware function:
      app.use(morgan('dev')) 
  1. Define a route method to handle all GET requests:
      app.get('*', (request, response, next) => { 
          response.send('Hello Morgan!') 
      }) 
  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 morgan-logger.js
  1. To see the result in your terminal, in your web browser, navigate to:
        http://localhost:1337/
        http://localhost:1337/example