Node.js语言
提示
本篇适用于 Node.js 服务端(脚本、Web 服务、CI 任务等)。浏览器环境的写法请看 使用 JavaScript 代码。
# 一、POST 发送(内置 https,零依赖)
不安装任何 npm 包,使用 Node 内置的 https 模块:
// push-demo.mjs (Node 14+,直接 node push-demo.mjs 运行)
import https from 'node:https';
const channelCode = '你的通道码';
const payload = JSON.stringify({
head: '测试标题', // 必填
body: '**构建结果**\n- 状态: 成功\n- 耗时: 38s', // 选填,支持 Markdown
url: 'https://example.com/ci/123' // 选填:点击跳转链接
});
const req = https.request(
{
hostname: 'www.phprm.com',
path: `/services/push/send/${channelCode}`,
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
'Content-Length': Buffer.byteLength(payload)
},
timeout: 10000
},
(res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
console.log('HTTP', res.statusCode);
console.log(data); // {"code":0,"message":"请求成功","data":{"messageIdList":[...]}}
});
}
);
req.on('error', (err) => console.error('推送失败:', err));
req.on('timeout', () => req.destroy(new Error('请求超时')));
req.write(payload);
req.end();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 二、POST 发送(axios,推荐项目中使用)
先安装依赖:npm install axios
// push-demo.mjs
import axios from 'axios';
async function sendPush(head, body, jumpUrl) {
const channelCode = '你的通道码';
try {
const { data, status } = await axios.post(
`https://www.phprm.com/services/push/send/${channelCode}`,
{
head: head,
body: body || '',
url: jumpUrl || ''
},
{
headers: { 'Content-Type': 'application/json;charset=UTF-8' },
timeout: 10000
}
);
console.log('HTTP', status, data);
return data;
} catch (err) {
// 必须明确报错,不要在失败时当成已发送
console.error('推送失败:', err.response ? err.response.data : err.message);
throw err;
}
}
sendPush('构建完成', '**分支**: main\n**耗时**: 38s', 'https://example.com/ci/123');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 三、GET 发送(最简单,只有 head 必填)
// push-get.mjs
import https from 'node:https';
const channelCode = '你的通道码';
const params = new URLSearchParams({
head: '测试标题',
body: '测试内容'
}).toString(); // 自动完成中文 UrlEncode
https
.get(`https://www.phprm.com/services/push/trigger/${channelCode}?${params}`, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => console.log(data));
})
.on('error', (err) => console.error('推送失败:', err));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 四、封装成可复用函数
// push.js
import axios from 'axios';
export function push(channelCode, { head, body = '', url = '' } = {}) {
if (!head) return Promise.reject(new Error('head 不能为空'));
return axios.post(
`https://www.phprm.com/services/push/send/${channelCode}`,
{ head, body, url },
{ headers: { 'Content-Type': 'application/json;charset=UTF-8' }, timeout: 10000 }
);
}
// 使用
// push('你的通道码', { head: '部署完成', body: '生产环境 v1.2.0 已发布' })
// .then(res => console.log(res.data));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
注意
- 通道码属于敏感凭证,建议放到环境变量(如
process.env.PUSH_CHANNEL_CODE)中,不要硬编码提交 git。 - POST 请求头需要
Content-Type: application/json;charset=UTF-8,且使用Buffer.byteLength计算中文内容长度。 - 服务端判断成功以响应 JSON 的
code === 0为准,失败时要记录并抛出,不能静默吞掉。
Last Updated: 2026/09/11, 21:21:27