---
url: /reference/functions/network/remote.md
---

# remote

`remote` 模块用于向远程服务发起 HTTP 请求，支持应用在 Nexus 函数内与远程后端服务进行集成。

导入：

```typescript
import { remote } from "@pc-nexus/network";
```

内置方法：

## invoke

使用 `invoke` 方法发起一个 HTTP 请求到配置的远程服务端点。

### 函数签名

```javascript
function invoke(remoteKey: string, options: RemoteInvokeOptions): Promise<Response>

type RemoteInvokeOptions = { path?: string; } & RequestInit;
```

### 参数

`RemoteInvokeOptions` 类型定义如下：

|名称|描述|
|---|---|
|`path`|请求路由地址|
|`method`|HTTP 请求方法|
|`headers`|自定义请求头|
|`body`|请求体内容|

### 返回值

返回标准的 `Response` 对象，可通过 `response.status` 、 `response.ok` 、 `response.json()` 、 `response.text()` 等标准方法处理响应。

### 示例

#### GET 请求

```javascript
import { remote } from "@pc-nexus/network";

const response = await remote.invoke(`my-remote-key`, {
    path: "/greeting?name=nexus",
    method: "GET"
});

const data = await response.json();
console.log(data);
```

#### POST 请求

```javascript
import { remote } from "@pc-nexus/network";

const response = await remote.invoke(`my-remote-key`, {
    path: "/work",
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        "Accept": "application/json",
    },
    body: JSON.stringify({
        team: "nexus",
    })
});

const status = response.status;
const result = await response.json();
```

### 错误处理

发生错误时响应都会附带一个包含更多信息的错误代码，下面列出了所有可能的错误代码、它们的含义以及可以采取哪些措施来解决。
