【问题标题】:CERT_HAS_EXPIRED - in post request to php from node jsCERT_HAS_EXPIRED - 在从节点 js 到 php 的 post 请求中
【发布时间】:2021-09-02 05:43:32
【问题描述】:

我正在尝试将数据发送到 php 脚本,然后将其保存在数据库中。但我收到一条错误消息 CERT_HAS_EXPIRED 今天是我第一次使用 node.js 这是我的 node.js 代码

const https = require("https");
const qs = require("querystring");

var postData = qs.stringify({
  name: "Bright Isaac",
  age: "21",
});

var options = {
  hostname: "localhost",
  port: 443,
  path: "/quelib/src/chats/post.php",
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "Content-Length": postData.length,
  },
};

var req = https.request(options, (res) => {
  console.log("statusCode:", res.statusCode);
  console.log("headers:", res.headers);

  res.on("data", (d) => {
    process.stdout.write(d);
  });
});

req.on("error", (e) => {
  console.error(e);
});

req.write(postData);
req.end();

错误

$ node index.js
Error: certificate has expired
    at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
    at TLSSocket.emit (events.js:315:20)
    at TLSSocket._finishInit (_tls_wrap.js:932:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12) {
  code: 'CERT_HAS_EXPIRED'

package.json

{
  "name": "qmyapp.com",
  "version": "1.0.0",
  "description": "chat",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Bright Isaac",
  "license": "ISC"
}

package-lock.json

{
  "name": "qmyapp.com",
  "version": "1.0.0",
  "lockfileVersion": 1
}

请问我该如何正确解决这个问题,也有人可以告诉我这是否是从节点 js 向 php 发送发布请求的正确方法。谢谢

P/S:如果您需要更多解释,请询问我

【问题讨论】:

    标签: node.js http-post


    【解决方案1】:

    快速但不安全

    仅用于调试目的!

    process.env.NODE_TLS_REJECT_UNAUTHORIZED='0'

    修复的最佳方法

    使用更新php站点的证书

    https://github.com/skoerfgen/ACMECert

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 2017-03-13
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      相关资源
      最近更新 更多