Using Morgan as a logging tool in HTTP servers implemented using Express & Node.js.Add to Pieces
Morgan in combination with Express & Node.js can be used to log requests, errors, and more to the console.
Tags: shell, mkdir
Used to create a new directory named 'test-morgan'.
Tags: npm, shell, reactjs, node.js
Used to initialize the node project with defaults, and will include your package.json file to include dependencies.
Installing morgan and express dependencies into the project.
Create entry file for the index of the project by using 'touch'.
Import express and morgan now that it has been added to your project, and start listening on Port 3000.
To use morgan in your Express server, you can invoke an instance and pass it as an argument in the .use() middleware before your HTTP requests. Morgan comes with a suite of presets, or predefined format strings, to create a new logger middleware with built-in format and options. The preset tiny provides a minimal output when logging HTTP requests.
The .token() method accepts a type, or the name of the token as the first argument, following a callback function. morgan will run the callback function each time a log occurs using the token. As a middleware, morgan applies the req and res objects as arguments. In your index.js file, employ the .token() method, and pass a type as the first argument following an anonymous function.
To denote custom arguments, you can use square brackets to define arguments passed to a token. This will allow your tokens to accept additional arguments. In your index.js file apply a custom argument to the morgan format string in the :param token.