Thomas Sentre

How to Use Morgan in Your Node.js Project

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.

Create a New Directory Named 'test-morgan'

Tags: shell, mkdir

Used to create a new directory named 'test-morgan'.

$ mkdir test-morgan

Related links:

  1. https://medium.com/gitconnected/how-to-use-morgan-in-your-nodejs-project-3d1a82de81ac
  2. https://newsletter.levelup.dev/
  3. https://github.com/expressjs/morgan#readme

Initialize a New Node Project with Defaults

Tags: npm, shell, reactjs, node.js

Used to initialize the node project with defaults, and will include your package.json file to include dependencies.

$ npm init --yes

Related links:

  1. https://medium.com/gitconnected/how-to-use-morgan-in-your-nodejs-project-3d1a82de81ac
  2. https://newsletter.levelup.dev/
  3. https://github.com/expressjs/morgan#readme

Install Morgan and Express as Dependency

Tags: angular, shell, node.js, npm, javascript

Installing morgan and express dependencies into the project.

$ npm install morgan express --save

Related links:

  1. https://medium.com/gitconnected/how-to-use-morgan-in-your-nodejs-project-3d1a82de81ac
  2. https://newsletter.levelup.dev/
  3. https://github.com/expressjs/morgan#readme

Create Entry File index.js

Tags: javascript

Create entry file for the index of the project by using 'touch'.

$ touch index.js

Related links:

  1. https://medium.com/gitconnected/how-to-use-morgan-in-your-nodejs-project-3d1a82de81ac
  2. https://newsletter.levelup.dev/
  3. https://github.com/expressjs/morgan#readme

Instantiate an Express Instance and Require Morgan

Tags: javascript, node, express, morgan

Import express and morgan now that it has been added to your project, and start listening on Port 3000.

import express from 'express';

import morgan from 'morgan';

const app=express();

app.listen(3000,()=>{

console.log('Listening on port 3000...')

})

Related links:

  1. https://medium.com/gitconnected/how-to-use-morgan-in-your-nodejs-project-3d1a82de81ac
  2. https://newsletter.levelup.dev/
  3. https://github.com/expressjs/morgan#readme

Calling app.use() Express Middleware with .morgan() as Argument

Tags: dart, nodejs, express, javascript

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.

app.use(morgan('tiny'));

Related links:

  1. https://medium.com/gitconnected/how-to-use-morgan-in-your-nodejs-project-3d1a82de81ac
  2. https://newsletter.levelup.dev/
  3. https://github.com/expressjs/morgan#readme

Index.js with .token() Method

Tags: javascript,  node.js, express

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.

import express from 'express';

import morgan from 'morgan';

const app=express();

morgan.token('host', function(req, res) {

return req.hostname;

});

// we are using the host parameter

app.use(morgan(':method :host :status :res[content-length] - :response-time ms'))

app.get("/", (req, res) => {

res.send("

Hello world!

"); }); app.listen(3000,()=>{ console.log('Listening on port 3000...') })

Related links:

  1. https://medium.com/gitconnected/how-to-use-morgan-in-your-nodejs-project-3d1a82de81ac
  2. https://newsletter.levelup.dev/
  3. https://github.com/expressjs/morgan#readme

Custom Argument in Morgan via :param token

Tags: javascript,  node.js, express

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.

app.use(morgan(':method :host :status :param[id] :res[content-length] - :response-time ms'));

morgan.token('param', function(req, res, param) {

return req.params[param];

});


Related links:

  1. https://medium.com/gitconnected/how-to-use-morgan-in-your-nodejs-project-3d1a82de81ac
  2. https://newsletter.levelup.dev/
  3. https://github.com/expressjs/morgan#readme