简体   繁体   中英

'Error: self signed certificate' with node+express application

Getting the below error

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',

I am running the application in my local system my code is below got stuck please help me some one hot to set cors in node.js application i have tried several things

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!');
});

If you are already importing the cors library use it instead of setting the headers for yourself.

var cors = require('cors');

If no credentials are needed:

app.use(cors();

If credentials are needed:

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

Now for your issue or rejecting self generated certificates, you can prevent these errors by using:

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;

Also, have a look at the https library from node

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM