上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.
- Create a new file named morgan-logger.js
- Initialize a new ExpressJS application:
const express = require('express') const morgan = require('morgan') const app = express()
- 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'))
- Define a route method to handle all GET requests:
app.get('*', (request, response, next) => { response.send('Hello Morgan!') })
- Listen on port 1337 for new connections:
app.listen( 1337, () => console.log('Web Server running on port 1337'), )
- Save the file
- Open a terminal and run:
node morgan-logger.js
- To see the result in your terminal, in your web browser, navigate to:
http://localhost:1337/ http://localhost:1337/example