通过这样做,我已经将我的 nodejs
服务器转换为使用 https
const https = require('https');
const privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
const certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
const app = express();
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, X-access-token');
next();
});
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(port)
.on('error', error => {
logger.error(error);
})
.on('listening', () => {
logger.info(`Express listening on ${port}`);
});
现在,当我使用 jquery 提供 html 文件时,API 调用工作正常,当我使用 postman
时,我需要从 settings 中禁用
并且它有效,现在我正在尝试在我的 ssl 证书验证
--> generallocalhost
上部署一个 angular 6
应用程序,而服务器在某个 IP 处处于 azure 状态,但我收到 ERR_CONNECTION_REFUSED
错误。我想如果 postman 通过删除验证选项来工作,我是否需要在 Angular 代码中为此做类似的事情?
目前API在https://137.xxx.xx.xx:8000/api/v0/......
最佳答案
您可以按如下方式使用“cors”包
$npm i -S cors
const cors = require('cors');
app.use(cors({origin:"http//:localhost:3000"}));
根据需要更改来源,具体取决于您将使用哪个来源调用电话。
关于node.js - 使用带有来自不同 IP 的自签名证书的 https 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925790/