<commit_before># express-request-capture
Node.js express middleware for capturing HTTP requests and responses

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]

## Install

```bash
npm install @clearonline/express-request-capture
```

## API

```js
var requestCapture = require ('@clearonline/express-request-capture')
```

### requestCapture ({ channel: string, url?: string })

When using this module with express or connect, simply `app.use` the module.
Request information `url, request, response, status, latency, and clientIp`, is printed/stored to the specified channel!
```js
var requestCapture = require ('express-request-capture'),
    express = require ('express')

var app = express()

var printAdapter = { channel: 'console|http|mongo|mysql', url: 'required if channel is either http or database' };
app.use(requestCapture(printAdapter))
```

```js
// sample response
{
    url: "https://alert.clearonline.org/api/v1/subscribe",
    method: "POST",
    status: 200,
    latency: 100,
    request: {
        headers: {
            "Content-Type": "application/json"
        },
        body: {
            email: "<EMAIL>",
            trigger: "solar energy"
        },
        host: "localhost:3000",
        clientIp: "192.168.127.12"
    },
    response: {
        header: {
            "Date": "2017-06-02T22:29:44.315Z"
        },
        body: {
            message: "Thank you for subscribing, i will send you notes every monday!"
        }
    }
}
```

## Examples

## License

[MIT](LICENSE)

[npm-image]: https://img.shields.io/npm/v/@clearonline/express-request-capture.svg?style=flat
[npm-url]: https://npmjs.org/package/@clearonline/express-request-capture
[downloads-image]: https://img.shields.io/npm/dm/@clearonline/express-request-capture.svg?style=flat
[downloads-url]: https://npmjs.org/package/@clearonline/express-request-capture


# Blog

## Description

Monitoring your web app is one the many ways to prevent hackers from breaking your app. In this tutorial, we do this by creating an expressjs middleware that logs all information related to the received request and return response.

## Goal

Capture all requests (request and response) that my express application handles.

## Step by Step

* initialize npm package

```sh
mkdir express-request-capture && cd express-request-capture
npm init -y
```

* create `index.js` file

```sh
# this will be the entry/main file of our middleware
touch index.js
```

* add content to `index.js` file

```js
module.exports = require('./src/capture.js')
```

* create the `src` folder

```sh
mkdir src
```

* create a `capture.js` file inside `src`

* put logic inside the `capture.js` file

* publish to npm

```sh
npm login
npm publish
```

* 6. use the middleware

```sh

```

<commit_msg>added change log on this repo<commit_after># express-request-capture
Node.js express middleware for capturing HTTP requests and responses

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]

## Install

```bash
npm install @clearonline/express-request-capture
```

## API

```js
var requestCapture = require ('@clearonline/express-request-capture')
```

### requestCapture ({ channel: string, url?: string })

When using this module with express or connect, simply `app.use` the module.
Request information `url, request, response, status, latency, and clientIp`, is printed/stored to the specified channel!
```js
var requestCapture = require ('express-request-capture'),
    express = require ('express')

var app = express()

var printAdapter = { channel: 'console|http|mongo|mysql', url: 'required if channel is either http or database' };
app.use(requestCapture(printAdapter))
```

```js
// sample response
{
    url: "https://alert.clearonline.org/api/v1/subscribe",
    method: "POST",
    status: 200,
    latency: 100,
    request: {
        headers: {
            "Content-Type": "application/json"
        },
        body: {
            email: "<EMAIL>",
            trigger: "solar energy"
        },
        host: "localhost:3000",
        clientIp: "192.168.127.12"
    },
    response: {
        header: {
            "Date": "2017-06-02T22:29:44.315Z"
        },
        body: {
            message: "Thank you for subscribing, i will send you notes every monday!"
        }
    }
}
```

## Examples

## License

[MIT](LICENSE)

[npm-image]: https://img.shields.io/npm/v/@clearonline/express-request-capture.svg?style=flat
[npm-url]: https://npmjs.org/package/@clearonline/express-request-capture
[downloads-image]: https://img.shields.io/npm/dm/@clearonline/express-request-capture.svg?style=flat
[downloads-url]: https://npmjs.org/package/@clearonline/express-request-capture


## Changelog
[Changelog of this project](CHANGELOG.md)
# Blog

## Description

Monitoring your web app is one the many ways to prevent hackers from breaking your app. In this tutorial, we do this by creating an expressjs middleware that logs all information related to the received request and return response.

## Goal

Capture all requests (request and response) that my express application handles.

## Step by Step

* initialize npm package

```sh
mkdir express-request-capture && cd express-request-capture
npm init -y
```

* create `index.js` file

```sh
# this will be the entry/main file of our middleware
touch index.js
```

* add content to `index.js` file

```js
module.exports = require('./src/capture.js')
```

* create the `src` folder

```sh
mkdir src
```

* create a `capture.js` file inside `src`

* put logic inside the `capture.js` file

* publish to npm

```sh
npm login
npm publish
```

* 6. use the middleware

```sh

```
{'fraction_non_alphanumeric': 0.13747579651469813, 'fraction_numerical': 0.014082027812004929, 'mean_word_length': 3.520286396181384, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6970288', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1694, 'n_words': 526}