# 内置函数 - JavaScript 代码节点
n8n 内置 JavaScript 函数和辅助工具的完整参考。
---
## 概述
n8n 代码节点提供了超越标准 JavaScript 的强大内置函数。本指南涵盖:
1. **$helpers.httpRequest()** - 发起 HTTP 请求
2. **DateTime (Luxon)** - 高级日期/时间操作
3. **$jmespath()** - 查询 JSON 结构
4. **$getWorkflowStaticData()** - 持久化存储
5. **标准 JavaScript 全局对象** - Math, JSON, console 等
6. **可用的 Node.js 模块** - crypto, Buffer, URL
---
## 1. $helpers.httpRequest() - HTTP 请求
直接从代码节点发起 HTTP 请求,无需使用 HTTP Request 节点。
### 基本用法
javascript
const response = await $helpers.httpRequest({
method: 'GET',
url: 'https://api.example.com/users'
});
return [{json: {data: response}}];
### 完整选项
javascript
const response = await $helpers.httpRequest({
method: 'POST', // GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
url: 'https://api.example.com/users',
headers: {
'Authorization': 'Bearer token123',
'Content-Type': 'application/json',
'User-Agent': 'n8n-workflow'
},
body: {
name: 'John Doe',
email: '
[email protected]'
},
qs: { // 查询字符串参数
page: 1,
limit: 10
},
timeout: 10000, // 毫秒 (默认:无超时)
json: true, // 自动解析 JSON 响应 (默认:true)
simple: false, // HTTP 错误时不抛出异常 (默认:true)
resolveWithFullResponse: false // 仅返回响应体 (默认:false)
});
### GET 请求
javascript
// 简单 GET
const users = await $helpers.httpRequest({
method: 'GET',
url: 'https://api.example.com/users'
});
return [{json: {users}}];
javascript
// 带查询参数的 GET
const results = await $helpers.httpRequest({
method: 'GET',
url: 'https://api.example.com/search',
qs: {
q: 'javascript',
page: 1,
per_page: 50
}
});
return [{json: results}];
### POST 请求
javascript
// 带 JSON 体的 POST
const newUser = await $helpers.httpRequest({
method: 'POST',
url: 'https://api.example.com/users',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + $env.API_KEY
},
body: {
name: $json.body.name,
email: $json.body.email,
role: 'user'
}
});
return [{json: newUser}];
### PUT/PATCH 请求
javascript
// 更新资源
const updated = await $helpers.httpRequest({
method: "