Tutorial

How To Use morgan in Your Express Project

Updated on December 24, 2020
Default avatar

By Cooper Makhijani

How To Use morgan in Your Express Project

Introduction

morgan is a Node.js and Express middleware to log HTTP requests and errors, and simplifies the process. In Node.js and Express, middleware is a function that has access to the request and response lifecycle methods, and the next() method to continue logic in your Express server.

In this article you will explore how to implement morgan in your Express project.

Prerequisites

To follow along with this article, you will need:

Step 1 – Setting Up the Project

As Express.js is a Node.js framework, ensure you have the latest version of Node.js from Node.js before moving forward.

To include morgan in your Express project, you will need to install it as a dependency.

Create a new directory named express-morgan for your project:

  1. mkdir express-morgan

Change into the new directory:

  1. cd express-morgan

Initialize a new Node project with defaults. This will include your package.json file to access your dependencies:

  1. npm init -y

Install morgan as a dependency:

  1. npm install morgan --save

Create your entry file, index.js. This is where you will handle logic in your Express server:

  1. touch index.js

Now that you’ve added morgan to your project, let’s include it in your Express server. In your index.js file, instantiate an Express instance and require morgan:

index.js
const express = require('express');
const morgan = require('morgan');

const app = express();

app.listen(3000, () => {
    console.debug('App listening on :3000');
});

With your Express server now set up, let’s look at using morgan to add request logging.

Step 2 – Using morgan in Express

To use morgan in your Express server, you can invoke an instance and pass 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 the minimal output when logging HTTP requests.

In your index.js file, invoke the app.use() Express middleware and pass morgan() as an argument:

index.js
const app = express();

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

Including the preset tiny as an argument to morgan() will use its built-in method, identify the URL, declare a status, and the request’s response time in milliseconds.

Alternatively, morgan reads presets like tiny in a format string defined below:

morgan(':method :url :status :res[content-length] - :response-time ms');

This tends to the same functionality contained in the tiny preset in a format that morgan parses. Following the : symbol are morgan functions called tokens. You can use the format string to define tokens create your own custom morgan middleware.

Step 3 – Creating Your Own Tokens

Tokens in morgan are functions identified following the : symbol. morgan allows you to create your own tokens with the .token() method.

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:

index.js
morgan.token('host', function(req, res) {
    return req.hostname;
});

The anonymous callback function will return the hostname on the req object as a new token to use in an HTTP request in your Express server.

Step 4 – Designing Tokens with Custom Arguments

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:

index.js
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];
});

The custom argument id on the :param token in the morgan invocation will include the ID in the parameter following the .token() method.

Conclusion

morgan allows flexibility when logging HTTP requests and updates precise status and response time in custom format strings or in presets. For further reading, check out the morgan documentation.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Cooper Makhijani

author



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel