出现以下错误
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!');
});
答案 0 :(得分:1)
如果您已经导入了cors库,请使用它代替自己设置标题。
var cors = require('cors');
如果不需要凭据:
app.use(cors();
如果需要凭据:
app.use(cors({credentials: true, origin: true}));
现在要解决的问题或拒绝自行生成的证书,您可以使用以下方法防止这些错误:
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;