【问题标题】:'Error: self signed certificate' with node+express application“错误:自签名证书”与 node+express 应用程序
【发布时间】:2018-08-30 07:28:25
【问题描述】:

得到以下错误

Error: self signed certificate
at TLSSocket.<anonymous> (_tls_wrap.js:1105:38)
at emitNone (events.js:106:13)
at TLSSocket.emit (events.js:208:7)
at TLSSocket._finishInit (_tls_wrap.js:639:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:469:38)
code: 'DEPTH_ZERO_SELF_SIGNED_CERT',

我正在本地系统中运行应用程序,我的代码如下 卡住了请帮我在node.js应用程序中设置cors我已经尝试了几件事

var express = require('express');
const axios = require('axios');
var cors = require('cors')
var app = express();
app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', req.get('Origin') || '*');
  res.header('Access-Control-Allow-Credentials', 'true');
  res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
  res.header('Access-Control-Expose-Headers', 'Content-Length');
  res.header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type, X-Requested-With, Range');
  if (req.method === 'OPTIONS') {
    return res.send(200);
  } else {
    return next();
  }
});

app.get('/', function (req, res) {
  res.send('Hello World!');
    axios.get('https://some-ip/api/v1/executions')
  .then(response => {
    console.log("-=-=-==- inside AXIOS");
    console.log(response.data);
    console.log("-=-=-=-=--=-=-=-=-SUCCESS");
    console.log(response.length);
  })
  .catch(error => {
    console.log("-=-=-==- inside ERRROR");
    console.log(error);
    console.log("-=-=-=-=--=-=-=-=-=---=-=-=-=--=-=-=-=-ERROR");

  });
});
app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

【问题讨论】:

  • “错误:自签名证书”错误与您的 CORS 配置无关。

标签: node.js reactjs


【解决方案1】:

如果您已经在导入 cors 库,请使用它而不是自己设置标头。

var cors = require('cors');

如果不需要凭据:

app.use(cors();

如果需要凭据:

app.use(cors({credentials: true, origin: true}));

现在对于您的问题或拒绝自行生成的证书,您可以使用以下方法防止这些错误:

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;

另外,看看https library from node

【讨论】:

    猜你喜欢
    • 2020-08-24
    • 2023-03-30
    • 2018-03-26
    • 1970-01-01
    • 2021-11-20
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多