得到以下错误
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库,请使用它而不是为自己设置标题。
var cors = require('cors');
如果不需要凭据:
app.use(cors();
如果需要凭据:
app.use(cors({credentials: true, origin: true}));
现在为您的问题或拒绝自己生成的证书,您可以通过使用以下方法来防止这些错误:
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;