

How To Use morgan in Your Express Project
source link: https://www.digitalocean.com/community/tutorials/nodejs-getting-started-morgan
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:
- A general understanding of Node.js is suggested, but not required. To learn more about Node.js, check out our How To Code in Node.js series.
- Node.js installed locally, which you can do by following How to Install Node.js and Create a Local Development Environment.
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:
mkdir express-morgan
Change into the new directory:
cd express-morgan
Initialize a new Node project with defaults. This will include your package.json
file to access your dependencies:
npm init -y
Install morgan as a dependency:
npm install morgan --save
Create your entry file, index.js
. This is where you will handle logic in your Express server:
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
:
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:
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:
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:
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.
Recommend
-
37
Do any GitHub or Google search for REST API structures using Node + Express and you’ll find very few of them follow the same organization. What’s even worse is, while there are lots of good tutorials out there, m...
-
5
20 Go Packages You Can Use in Your Next ProjectImprove your application and save your time.
-
16
Introduction Short for request, the req object is one half of the request and
-
10
Tutorial How To Use JSON Web Tokens (JWTs) in Express.js Node.js ...
-
3
-
7
What Does `app.use(express.json())` Do in Express? Dec 20, 2021 The app.use() function adds a new
-
5
DbUnit Express 1.3 is Even Easier to Use and Still Better September 4, 2011 The
-
3
How to use HTTP2 with Express.js and test it locallyNick ScialliDecember 03, 2022Express.js is surprisingly lacking in HTTP/2 support. I have yet to find a resource that explains how to create an Express server with HTTP/2 support...
-
3
JP Morgan cracks down on traders’ use of ChatGPT Investment bank places restrictions on chatbots over data security fears...
-
4
Texpress Texpress is a project setup for using ExpressJS with Typescript. It includes a monorepo architecture and uses turborepo for managing the codebase. For Complete Documentation
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK