gpt4 book ai didi

node.js - 如何修复错误 "net::ERR_SSL_SERVER_CERT_BAD_FORMAT"将 vuejs 和 nodejs 与 https 和 express 一起使用时

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:19 25 4
gpt4 key购买 nike

我有一些代码可以在 vue.js 中发送 https 请求,当在 vuex 中使用操作方法发送 https 请求时,我在控制台中收到此错误

GET https://localhost/api/getpeople net::ERR_SSL_SERVER_CERT_BAD_FORMAT

我的代码是:

vue.js 表格.js

 import Axios from "axios";

let state = {
people: []
};

let getters = {
getPeople(state) {
return state.people;
}
}

let mutations = {

setPeople(state, people) {

state.people = people
}

}

let actions = {
sendHttpReq({ commit }) {
Axios.get('https://localhost:443/api/getpeople').then(response=>response.data).then(result=>{
commit('setPeople',result);
}).catch(error=>{
console.log(error.response)
})
}
}

export default {
state,
getters,
mutations,
actions
}

Node.js 服务器端:

let express=require('express');
let cors=require('cors');
let https=require('https');
let pem=require('pem');
let mydb=require('./mydb')
pem.createCertificate({days:1,selfSigned: true},(err,keys)=> {

if (err)
return err;


let app = express();
app.use(express.json());
app.use(cors());
app.post('/api/setPeople', (req, res) => {
let body = req.body;
mydb.insert(body.firstName, body.lastName, body.phone, (result) => {
res.status(200).send(result)
});


});
app.get('/api/getpeople', async (req, res) => {
mydb.getPoeple((result) => {
console.log(result);
res.status(200).send(result)
});
});

https.createServer({key: keys.serviceKey, cert: keys.certificate}, app).listen(443, () => {
console.log('server is run ' + 443);
});

})

这是发送 https 请求并获得响应并设置为人员的代码,其他代码将人员显示到表格中。其他代码是正确的,但是这个代码有问题

我得到 console.log(key.certificate) 并得到这个结果:

-----开始证书-----
MIICpDCCAYwCCQD1yVw3YCtIUDANBgkqhkiG9w0BAQsFADAUMRIweEAYDVQQDDAls
b2NhbGhvc3QwHhcNMTkxMDA2MTgxNzE3WhcNMTkxMDA3MTgxNzE3WjAUMRIweYD
VQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDc
e+2PKex1g7qkKljtWD9JgP7MBgL/YTsmMj3TGtn1cmV0415jb8tSJZi8x8zJwudY
pDAjxk4bCRud0maV4Ag3LNSC8R+GrVpMd5oPzFI9crATf5OHzyJWhb3qYAutkw3s
GB78q9VoFZygwV7LF2nAU61z6VS/mwECohEoJUvUSvcMmt4Qa3IBrFxpJhf5K6B8
kLRYzhM/FpRxBGql9vuSYZWIpgWTpOIdUNwUtDejNE35CzrV8fhKzQWVEPQUSX3D
7wJVIa5YBtJnxmPAIthiDTR6Z/N8VTccWJgWXxJsJ8qxIl1jn3xkOvaGRo2PyeVW
+baSzEu6jYYkcSWj6DWJAgMBAAEwDQYJKoZIhvcNAQELBQADggEBABe9xrSwiJqW
TUpgjc2mhXjsFlAZ9E1tkd3X+rayqfT236fsqtI0oifbCHtcSVGAxS9mu8rrSjLr
uLOA8Guiod+pLvyizf1vZHYX6PAFiUOrOSj6i1IPN911yhMTrD1c9F1nHGuaklSv
De+A5Vqu0VZdoZx2mtfZthILerqBr/isMweeTdrTOedbLz9+AbtrEpowEEdytH0
kOpljE0ndoPoqY7Q/CbZq8GlI6Zg504wDuYhUcFAnPgAoY+MWhP/+wquCbnlQfVD
/DlWQh51Y+rpUghrf3GNenF58StvD7XpYIwCItpw2F3eWluB8QfDoRJ9rVTtEevA
S+44fP5pe4U=
-----证书结束-----

最佳答案

获取问题中显示的证书 PEM,可以执行 openssl x509 -text 并查看:

Certificate:
Data:
Version: 1 (0x0)
...
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN=localhost
...
Subject: CN=localhost

因此,这是为本地主机颁发的 X509v1 证书。它没有至少 Chrome 要求的任何主题备用名称扩展。只有 X509v3 证书可以有这样的扩展,它们需要专门配置。 documentation of pem包含有关如何创建具有必要扩展的证书的示例。

关于node.js - 如何修复错误 "net::ERR_SSL_SERVER_CERT_BAD_FORMAT"将 vuejs 和 nodejs 与 https 和 express 一起使用时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58248360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com