我有一个运行NodeJS和ExpressJS的客户端服务器,它对后端API服务器进行API调用。但是,只要使用fetch()
进行API调用,后端API服务器就会抛出自签名证书错误警告。
如何忽略自签名证书警告并让浏览器始终信任后端API服务器以进行API调用?
这是错误:
以下是我使用fetch进行API调用的方法:
logInUser(userCredentials) {
var userCredsBody = {
'firstname': `${userCredentials.username}`,
'password': `${userCredentials.password}`
}
var configuration = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userCredsBody)
}
return function(dispatch) {
fetch('https://serverapi.com:8080/userInfo', configuration)
.then(response => response.json())
.then(response => {
console.log('Successful and ', response)
})
.catch((error) => {
console.log("Error: ", error)
})
}
以下是我的客户端NodeJS和ExpressJS的设置方式:
var express = require('express');
var cors = require('cors');
var path = require('path');
var config = require('../webpack.config.js');
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var app = express();
var compiler = webpack(config);
app.use(cors());
app.use(webpackDevMiddleware(compiler, {noInfo: true, publicPath: config.output.publicPath}));
app.use(webpackHotMiddleware(compiler));
app.use(express.static('./dist'));
app.use('/', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.sendFile(path.resolve('client/index.html'))
})
var port = 3000;
app.listen(port, function(error) {
if (error) throw error;
console.log("Express server listening on port", port);
});