You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中使用o.js实现HTTPS双向认证的示例请求

Node.js 使用 o.js 配置HTTPS证书连接OData服务示例

o.js 底层依赖 axios 发送请求,因此可以通过配置 axios 的 httpsAgent 传入客户端证书,实现HTTPS双向认证的OData服务调用。以下是适配你现有证书配置的完整示例:

const o = require('o.js');
const https = require('https');
const fs = require('fs');

// 加载证书文件,和你原代码逻辑一致
const sslConfig = {
  key: fs.readFileSync("path/to/client-key.pem"),
  cert: fs.readFileSync("path/to/client-crt.pem"),
  ca: fs.readFileSync("path/to/ca-crt.pem"),
  rejectUnauthorized: false, // 注意:生产环境必须改为true,禁用此参数会带来安全风险
};

// 创建HTTPS Agent,供axios复用证书配置
const httpsAgent = new https.Agent(sslConfig);

// 初始化o.js客户端,配置OData服务基础地址和HTTPS代理
const client = o({
  baseUrl: "https://<the hostname>:44300", // 替换为你的服务根地址
  axios: {
    httpsAgent: httpsAgent
  }
});

// 示例1:GET请求获取实体集数据(无需手动拼接OData URL)
client.get("<EntitySetName>") // 替换为你的实体集名称
  .then(response => {
    console.log("响应数据:", response.data);
    // 这里处理你的业务逻辑
  })
  .catch(error => {
    console.error("请求失败:", error);
  });

// 示例2:带过滤条件的GET请求(o.js自动生成OData过滤语法)
client.get("<EntitySetName>")
  .filter({ Id: 123, Status: "Active" })
  .select(["Id", "Name", "Status"])
  .then(response => {
    console.log("过滤后的响应:", response.data);
  });

// 示例3:POST请求创建新实体
client.post("<EntitySetName>", {
  Name: "新实体",
  Description: "通过o.js创建的测试实体"
})
  .then(response => {
    console.log("创建成功,实体ID:", response.data.Id);
  });

关键说明:

  • baseUrl 直接配置OData服务的根地址,后续调用实体集时只需传入名称,o.js会自动拼接完整的OData URL
  • httpsAgent 复用了你原代码中的证书配置,确保HTTPS双向认证生效
  • 生产环境务必将 rejectUnauthorized 设为 true,避免绕过证书验证带来的中间人攻击风险
  • o.js支持链式调用构建复杂OData查询(过滤、排序、选择字段等),无需手动拼接查询参数

内容的提问来源于stack exchange,提问作者DJDaveMark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 10:20:06