From 0a4aeb9a68f86acc741e316d583f71688b2a64ea Mon Sep 17 00:00:00 2001 From: Ryan Hinchey Date: Thu, 9 Apr 2020 20:20:46 -0400 Subject: [PATCH] adding custom token example and moving new token docs to bottom of token section --- README.md | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index fc1c4b4..dec7290 100644 --- a/README.md +++ b/README.md @@ -137,25 +137,6 @@ The minimal output. #### Tokens -##### Creating new tokens - -To define a token, simply invoke `morgan.token()` with the name and a callback function. -This callback function is expected to return a string value. The value returned is then -available as ":type" in this case: - - - -```js -morgan.token('type', function (req, res) { return req.headers['content-type'] }) -``` - -Calling `morgan.token()` using the same name as an existing token will overwrite that -token definition. - -The token function is expected to be called with the arguments `req` and `res`, representing -the HTTP request and HTTP response. Additionally, the token can accept further arguments of -it's choosing to customize behavior. - ##### :date[format] The current date and time in UTC. The available formats are: @@ -228,6 +209,33 @@ The URL of the request. This will use `req.originalUrl` if exists, otherwise `re The contents of the User-Agent header of the request. +##### Creating new tokens + +To define a new token, invoke `morgan.token()` with the name and a callback function. +The callback function will be called with two arguments `req`, and `res`, +where `req` is the HTTP request and `res` is the HTTP response. This callback function is expected to return a string value. + +For example, you would define a token named `type` as follows: + + + +```js +morgan.token('type', function (req, res) { return req.headers['content-type'] }) +``` + +The token `type` can then be used in format strings and is included in the `tokens` object passed to custom format functions: + + + +```js +morgan(':method :url :type') +``` + +Additionally, the token function can accept further arguments of it's choosing to customize behavior. They can be passed using `[]`, for example: `:token-name[pretty]` would pass the string `'pretty'` as an argument to the token `token-name`. + +Calling `morgan.token()` using the same name as an existing token will overwrite that +token definition. + ### morgan.compile(format) Compile a format string into a `format` function for use by `morgan`. A format string