# @cloudgyb/http-filter

> HTTP request and response filter for NodeJS.It is an implementation of the filter design pattern.

Latest version **1.0.2** (published 2022-03-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install @cloudgyb/http-filter
pnpm add @cloudgyb/http-filter
yarn add @cloudgyb/http-filter
bun add @cloudgyb/http-filter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-03-15 |
| First published | 2022-03-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | cloudgyb |
| Keywords | http filter, http-filter, request filter, request-filter, req filter, req-filter, http, filter |

## Links

- npm: https://www.npmjs.com/package/@cloudgyb/http-filter
- Repository: https://github.com/cloudgyb/http-filter
- Issues: https://github.com/cloudgyb/http-filter/issues
- npm.io page: https://npm.io/package/@cloudgyb/http-filter

## Dependencies (1)

- [ant-path-matcher](https://npm.io/package/ant-path-matcher.md) ^0.0.5

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2022-03-15
- 1.0.0 — 2022-03-15

## README

# Http-filter

HTTP request and response filter for NodeJS.It is an implementation of the filter design pattern.
```


    +++++++  req  +++++++++++        +++++++++++      +++++++++++       ++++++++++++++++++++
    | req | ----> |         |  --->  |         | ---> |         | --->  |                  |
--> | res |  res  | filter1 |        | filter2 |      | filterN |       | business handler | 
    +++++++ <---- |         |  <---  |         | <--- |         | <---  |                  |  
                  +++++++++++        +++++++++++      +++++++++++       ++++++++++++++++++++
             
```
## Install

```shell
npm i @cloudgyb/http-filter
```

## Usage

```javascript
const HttpFilter = require('http-filter');
const http = require('http');

/**
 * Prevent IFRAME nesting by add 'X-Frame-Option' response header.
 * @author cloudgyb
 */
class IFrameHeaderFilter extends HttpFilter.Filter {
    constructor(pattern) {
        super(pattern);
    }

    doFilter(req, resp, filterChain) {
        resp.setHeader("X-Frame-Option", "SAMEORIGIN");
        filterChain.doFilter(req, resp);
    }
}

/**
 * ip access control, disable access to the specified IP
 * @author cloudgyb
 */
class IPAccessControlFilter extends HttpFilter.Filter {
    constructor(pattern) {
        super(pattern);
    }

    doFilter(req, resp, filterChain) {
        let remoteAddress = req.socket.remoteAddress;
        if (remoteAddress === "192.168.1.121") {
            resp.writeHead(400);
            resp.write("  ");
            resp.end();
            return;
        }
        filterChain.doFilter(req, resp);
    }
}

//The order in which filters are executed depends on the index of the following array elements.
const filters = []
filters.push(new IPAccessControlFilter("/**"));
filters.push(new IFrameHeaderFilter("/**"));

//create a http server and listen at 80
http.createServer((req, res) => {
    const filterChain = new HttpFilter.FilterChain(filters);
    filterChain.doFilter(req, res);
    res.writeHead(200, {'content-type': 'text/html'})
    res.end("<h2>OK!<h2>");
}).on("listening", () => {
    console.log("process listen at " + 80 + "...")
}).listen(80);
```

---
_Source: https://npm.io/package/@cloudgyb/http-filter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
