# 4.2.9 命名空间中间件函数 namespace\_use\_fn

* `fn` (Function)

注册一个中间件，它是一个为每个传入Socket 执行的函数，并作为参数接收socket和一个函数，以选择将执行推迟到下一个注册的中间件。

传递到中间件回调的错误作为特殊错误包发送到客户端。

```javascript
io.use((socket,next)=>{
    if (socket.request.header.cookie) return next();
    next(new Error('authentication error'));
})
```


---

# 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://socket.gitbook.io/docs/4-fu-wu-duan-apiserverapi/4.2-ming-ming-kong-jian-namespace/namespace_use_fn.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.
