Skip to content

Commit

Permalink
adding custom token example and moving new token docs to bottom of to…
Browse files Browse the repository at this point in the history
…ken section
  • Loading branch information
ryhinchey committed Apr 10, 2020
1 parent c68d2ea commit 0a4aeb9
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions README.md
Expand Up @@ -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:

<!-- eslint-disable no-undef -->

```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:
Expand Down Expand Up @@ -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:

<!-- eslint-disable no-undef -->

```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:

<!-- eslint-disable no-undef -->

```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
Expand Down

0 comments on commit 0a4aeb9

Please sign in to comment.