Demo代码(JavaScript Node.js)
阿星
发布于:2025-06-17
const axios = require('axios');
async function createTask() {
try {
const res = await axios.post(
'https://api.comfylink.com/v1/openapi/tasks',
{
remark: '',
appId: 'YOUR_APPID',
inputs: [{ name: '6:text', value: 'test ...' }]
},
{
headers: {
'X-API-Key': 'YOUR_API_KEY',
'X-Client-Id': 'YOUR_UUID'
},
timeout: 10000 // 10秒超时,防止卡死
}
);
if (res.data.code === 0) {
console.log('任务创建成功,任务ID:', res.data.data.taskId);
} else {
console.error('任务创建失败:', res.data.msg);
}
} catch (err) {
if (err.response) {
// 服务端有响应但返回错误码
console.error('请求失败:', err.response.status, err.response.data);
} else if (err.request) {
// 请求已发出但无响应
console.error('无响应:', err.message);
} else {
// 其他错误
console.error('请求出错:', err.message);
}
}
}
createTask();
const axios = require('axios');
async function cancelTask() {
const taskId = '创建任务返回的任务ID'; // 替换为实际任务ID
const url = `https://api.comfylink.com/v1/openapi/tasks/${taskId}`;
const headers = {
'X-API-Key': 'YOUR_API_KEY',
'X-Client-Id': 'YOUR_UUID'
};
try {
const res = await axios.delete(url, { headers, timeout: 10000 });
if (res.data.code === 0) {
console.log('任务已取消');
} else {
console.error('取消任务失败:', res.data.msg);
}
} catch (err) {
if (err.response) {
// 服务端有响应但返回错误码
console.error('请求失败:', err.response.status, err.response.data);
} else if (err.request) {
// 请求已发出但无响应
console.error('无响应:', err.message);
} else {
// 其他错误
console.error('请求出错:', err.message);
}
}
}
cancelTask();
const axios = require('axios');
async function queryTask() {
const taskId = '创建任务返回的任务ID'; // 替换为实际任务ID
const url = `https://api.comfylink.com/v1/openapi/tasks/${taskId}`;
const headers = {
'X-API-Key': 'YOUR_API_KEY',
'X-Client-Id': 'YOUR_UUID'
};
try {
const res = await axios.get(url, { headers, timeout: 10000 });
if (res.data.code === 0) {
const data = res.data.data || {};
switch (data.status) {
case 'finished':
console.log('任务完成,输出:', data.outputs);
break;
case 'failed':
console.log('任务失败:', data.message);
break;
case 'canceled':
console.log('任务已取消:', data.message);
break;
case 'pending':
console.log('任务排队中:', data.message);
break;
default:
console.log('未知状态:', data.status, '描述:', data.message);
}
} else {
console.error('查询失败:', res.data.msg);
}
} catch (err) {
if (err.response) {
// 服务端有响应但返回错误码
console.error('请求失败:', err.response.status, err.response.data);
} else if (err.request) {
// 请求已发出但无响应
console.error('无响应:', err.message);
} else {
// 其他错误
console.error('请求出错:', err.message);
}
}
}
queryTask();
const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');
const path = require('path');
async function uploadFile() {
const filePath = 'comfylink.jpg'; // 替换为实际文件路径
if (!fs.existsSync(filePath)) {
console.error('文件不存在:', filePath);
return;
}
const url = 'https://api.comfylink.com/v1/openapi/files';
const headers = {
'X-API-Key': 'YOUR_API_KEY',
'X-Client-Id': 'YOUR_UUID'
};
const form = new FormData();
form.append('file', fs.createReadStream(filePath), path.basename(filePath));
try {
const res = await axios.post(url, form, {
headers: { ...headers, ...form.getHeaders() },
maxContentLength: Infinity,
maxBodyLength: Infinity,
timeout: 20000 // 20秒超时
});
if (res.data.code === 0) {
console.log('文件上传成功,路径:', res.data.data.path);
} else {
console.error('文件上传失败:', res.data.msg);
}
} catch (err) {
if (err.response) {
// 服务端有响应但返回错误码
console.error('请求失败:', err.response.status, err.response.data);
} else if (err.request) {
// 请求已发出但无响应
console.error('无响应:', err.message);
} else {
// 其他错误
console.error('请求出错:', err.message);
}
}
}
uploadFile();
const WebSocket = require('ws');
const ws = new WebSocket('wss://api.comfylink.com/v1/openapi/ws', {
headers: {
'X-API-Key': 'YOUR_API_KEY',
'X-Client-Id': 'YOUR_UUID'
}
});
ws.on('open', () => {
console.log('WebSocket 连接已建立');
});
ws.on('message', (data) => {
let message;
try {
message = JSON.parse(data.toString());
} catch (e) {
console.error('JSON 解析失败:', data.toString());
return;
}
const dataField = message.data || {};
const status = dataField.status;
const msg = dataField.message || '';
switch (status) {
case 'finished':
console.log('【任务完成】');
console.log('输出结果:', dataField.outputs);
break;
case 'failed':
console.log('【任务失败】错误信息:', msg);
break;
case 'canceled':
console.log('【任务已取消】原因:', msg);
break;
case 'pending':
console.log('【任务排队中】当前状态:', msg);
break;
default:
console.log('未知状态:', status, '消息:', msg);
}
});
ws.on('error', (err) => {
console.error('发生错误:', err.message, err);
});
ws.on('close', (code, reason) => {
console.log(`连接已关闭,代码: ${code},原因: ${reason}`);
});