# ngx\_stream\_js\_module

* [示例配置](/nginx-docs/he-xin-gong-neng/stream/ngx_stream_js_module.md#example_configuration)
* [指令](/nginx-docs/he-xin-gong-neng/stream/ngx_stream_js_module.md#directives)
  * [js\_access](/nginx-docs/he-xin-gong-neng/stream/ngx_stream_js_module.md#js_access)
  * [js\_filter](/nginx-docs/he-xin-gong-neng/stream/ngx_stream_js_module.md#js_filter)
  * [js\_include](/nginx-docs/he-xin-gong-neng/stream/ngx_stream_js_module.md#js_include)
  * [js\_preread](/nginx-docs/he-xin-gong-neng/stream/ngx_stream_js_module.md#js_preread)
  * [js\_set](/nginx-docs/he-xin-gong-neng/stream/ngx_stream_js_module.md#js_set)
* [会话对象属性](/nginx-docs/he-xin-gong-neng/stream/ngx_stream_js_module.md#properties)

`ngx_stream_js_module` 模块用于在 [njs](/nginx-docs/readme/guan-yu-nginscript.md) 中实现处理程序 —— 这是 JavaScript 语言的一个子集。

默认情况下不构建此模块。可在[此处](http://nginx.org/en/docs/njs/install.html)下载和安装说明。

> 此示例适用于 njs [0.2.4](http://nginx.org/en/docs/njs/changes.html#njs0.2.4) 及更高版本。对于 njs [0.2.3](http://nginx.org/en/docs/njs/changes.html#njs0.2.3) 及更早版本，请使用[此示例](http://nginx.org/en/docs/njs/examples.html#legacy)。

## 示例配置 <a href="#example_configuration" id="example_configuration"></a>

```
load_module modules/ngx_stream_js_module.so;
...

stream {
    js_include stream.js;

    js_set $bar bar;
    js_set $req_line req_line;

    server {
        listen 12345;

        js_preread preread;
        return     $req_line;
    }

    server {
        listen 12346;

        js_access  access;
        proxy_pass 127.0.0.1:8000;
        js_filter  header_inject;
    }
}

http {
    server {
        listen 8000;
        location / {
            return 200 $http_foo\n;
        }
    }
}
```

`stream.js` 内容：

```javascript
var line = '';

function bar(s) {
    var v = s.variables;
    s.log("hello from bar() handler!");
    return "bar-var" + v.remote_port + "; pid=" + v.pid;
}

function preread(s) {
    s.on('upload', function (data, flags) {
        var n = data.indexOf('\n');
        if (n != -1) {
            line = data.substr(0, n);
            s.done();
        }
    });
}

function req_line(s) {
    return line;
}

// Read HTTP request line.
// Collect bytes in 'req' until
// request line is read.
// Injects HTTP header into a client's request

var my_header =  'Foo: foo';
function header_inject(s) {
    var req = '';
    s.on('upload', function(data, flags) {
        req += data;
        var n = req.search('\n');
        if (n != -1) {
            var rest = req.substr(n + 1);
            req = req.substr(0, n + 1);
            s.send(req + my_header + '\r\n' + rest, flags);
            s.off('upload');
        }
    });
}

function access(s) {
    if (s.remoteAddress.match('^192.*')) {
        s.abort();
        return;
    }

    s.allow();
}
```

## 指令 <a href="#directives" id="directives"></a>

### js\_access

|       - | 说明                         |
| ------: | -------------------------- |
|  **语法** | **js\_access** `function`; |
|  **默认** | ——                         |
| **上下文** | stream、server              |

设置一个将在 [access](/nginx-docs/readme/nginx-ru-he-chu-li-tcpudp-hui-hua.md) 阶段调用的 njs 函数。

### js\_filter

|       - | 说明                         |
| ------: | -------------------------- |
|  **语法** | **js\_filter** `function`; |
|  **默认** | ——                         |
| **上下文** | stream、server              |

设置一个数据过滤器。

### js\_include

|       - | 说明                      |
| ------: | ----------------------- |
|  **语法** | **js\_include** `file`; |
|  **默认** | ——                      |
| **上下文** | stream                  |

指定一个使用 njs 实现服务器和变量处理程序的文件。

### js\_preread

|       - | 说明                          |
| ------: | --------------------------- |
|  **语法** | **js\_preread** `function`; |
|  **默认** | ——                          |
| **上下文** | stream、server               |

设置一个将在 \[preread]\((../../介绍/Nginx如何处理TCP\_UDP会话.md)) 阶段调用的 njs 函数。

### js\_set

|       - | 说明                      |
| ------: | ----------------------- |
|  **语法** | **js\_set** `function`; |
|  **默认** | ——                      |
| **上下文** | stream                  |

设置一个用于指定变量的 njs 函数。

## 会话对象属性 <a href="#properties" id="properties"></a>

每一个流 njs 处理程序都会接收一个参数，一个流会话[对象](http://nginx.org/en/docs/njs/reference.html#stream)。

## 原文档

<http://nginx.org/en/docs/stream/ngx_stream_js_module.html>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docshome.gitbook.io/nginx-docs/he-xin-gong-neng/stream/ngx_stream_js_module.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
