node.js - 如何修复错误“net :: ERR_SSL_SERVER_CERT_BAD_FORMAT”将vuejs和nodejs与https和express一起使用时
问题描述
我有一些代码可以在 vue.js 中发送 https 请求,当使用 vuex 中的操作方法发送 https 请求时,我在控制台中收到此错误
获取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) 并得到这个结果:
-----BEGIN CERTIFICATE-----
MIICpDCCAYwCCQD1yVw3YCtIUDANBgkqhkiG9w0BAQsFADAUMRIwEAYDVQQDDAls
b2NhbGhvc3QwHhcNMTkxMDA2MTgxNzE3WhcNMTkxMDA3MTgxNzE3WjAUMRIwEAYD
VQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDc
e+2PKex1g7qkKljtWD9JgP7MBgL/YTsmMj3TGtn1cmV0415jb8tSJZi8x8zJwudY
pDAjxk4bCRud0maV4Ag3LNSC8R+GrVpMd5oPzFI9crATf5OHzyJWhb3qYAutkw3s
GB78q9VoFZygwV7LF2nAU61z6VS/mwECohEoJUvUSvcMmt4Qa3IBrFxpJhf5K6B8
kLRYzhM/FpRxBGql9vuSYZWIpgWTpOIdUNwUtDejNE35CzrV8fhKzQWVEPQUSX3D
7wJVIa5YBtJnxmPAIthiDTR6Z/N8VTccWJgWXxJsJ8qxIl1jn3xkOvaGRo2PyeVW
+baSzEu6jYYkcSWj6DWJAgMBAAEwDQYJKoZIhvcNAQELBQADggEBABe9xrSwiJqW
TUpgjc2mhXjsFlAZ9E1tkd3X+rayqfT236fsqtI0oifbCHtcSVGAxS9mu8rrSjLr
uLOA8Guiod+pLvyizf1vZHYX6PAFiUOrOSj6i1IPN911yhMTrD1c9F1nHGuaklSv
De+A5Vqu0VZdoZx2mtfZthILerqBr/iSMweeTdrTOedbLz9+AbtrEpowEUedytH0
kOpljE0ndoPoqY7Q/CbZq8GlI6Zg504wDuYhUcFAnPgAoY+MWhP/+wquCbnlQfVD
/DlWQh51Y+rpUghrf3GNenF58StvD7XpYIwCItpw2F3eWluB8QfDoRJ9rVTtEevA
S+44fP5pe4U=
-----END CERTIFICATE-----
解决方案
axios
执行GET
请求,您应该在 url 中发送数据
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
})
并执行POST
请求
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
现在在您的代码中您发送 POST 请求并且这有要发送的对象,您应该使用
Axios.post('https://localhost:443/api/getpeople',{ withCredentials:
true}).then(response=>response.data).then(result=>{
commit('setPeople',result);
}).catch(error=>{
console.log(error.response)
对于 GET 请求
Axios.get('https://localhost:443/api/getpeople').then(response=>response.data).then(result=>{
commit('setPeople',result);
}).catch(error=>{
console.log(error.response)
推荐阅读
- c# - 如何让我的体力慢慢恢复?
- node.js - 从 db 请求数据时无法读取未定义的属性“查找”
- javascript - React native - Function CollectionReference.doc() 需要它的第一个参数
- fft - 使用 fft 的二阶导数
- android-viewpager - ViewPager2 迁移后出现无效 ID 0x00000001 错误
- go - 如何添加两个curve25519点
- flutter - 如何在电话身份验证期间删除移动设备中的 reCaptcha 屏幕?
- google-apps-script - Gmail 插件 - 将邮件错误地视为 - “不能使用垃圾邮件和可疑邮件”
- java - Spring Cloud 配置服务器启动失败
- python-3.x - 如何将数据框中的列替换为函数的结果