使用https时如何解决vuejs和nodejs中的错误“ net :: ERR_SSL_SERVER_CERT_BAD_FORMAT”

时间:2019-10-05 12:37:41

标签: node.js vue.js ssl https vuex

我有一些代码在vue.js中发送https请求,当在vuex中使用动作方法发送https请求时,在控制台中出现此错误

  

获取https://localhost/api/getpeople   净:: ERR_SSL_SERVER_CERT_BAD_FORMAT

我的代码是:

vue.js table.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-----

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

采用问题一所示的证书PEM可以进行openssl x509 -text并查看:

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

因此,这是为本地主机颁发的X509v1证书。至少没有Chrome所要求的,它没有任何使用者替代名称扩展名。仅X509v3证书可以具有此类扩展名,并且需要对其进行专门配置。 documentation of pem包含有关如何创建具有必要扩展名的证书的示例。