2017-10-11 1 views
1

Je suis en train de programmer une application Web avec AngularJS et j'utilise NodeJS en raison de problèmes CORS. Eh bien, quand je suis en train de faire la demande dans mon noeud je reçois cette erreur:NodeJS. Erreur: impossible de vérifier le premier certificat

events.js:160 
     throw er; // Unhandled 'error' event 
    ^

Error: unable to verify the first certificate 
    at Error (native) 
    at TLSSocket.<anonymous> (_tls_wrap.js:1092:38) 
    at emitNone (events.js:86:13) 
    at TLSSocket.emit (events.js:185:7) 
    at TLSSocket._finishInit (_tls_wrap.js:610:8) 
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38) 

Mon fichier NodeJS (index.js):

var express = require('express') 
var cors = require('cors') 
var bodyparser = require('body-parser'); 
var querystring = require('querystring'); 
var rootCas = require('ssl-root-cas/latest').create(); 
rootCas.addFile(__dirname + '/ssl/ovccatastromehes.crt'); 
const https = require('https'); 
https.globalAgent.options.ca = rootCas; 
var request = require('request'); 

var app = express() 

app.use(bodyparser.urlencoded({extened:false})); 
app.use(bodyparser.json()); 
app.use(function (req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080'); 

    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 

    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Authorization'); 

    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 

    // Pass to next layer of middleware 
    next(); 
}); 


app.get('/space/:id', cors(), function (req, res) { 
    var soapRequest = '<?xml version="1.0" encoding="utf-8"?>' 
      +'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' 
      +'<soap:Body>' 
       +'<Provincia xmlns="http://www.catastro.meh.es/"></Provincia>' 
       +'<Municipio xmlns="http://www.catastro.meh.es/"></Municipio>' 
       +'<SRS xmlns="http://www.catastro.meh.es/">EPSG:4326</SRS>' 
       +'<RefCat xmlns="http://www.catastro.meh.es/">'+req.params.id+'</RefCat>' 
      +'</soap:Body></soap:Envelope>'; 

    var options = { 
     host: 'ovc.catastro.meh.es', 
     path: '/ovcservweb/ovcswlocalizacionrc/ovccoordenadas.asmx', 
     method: 'POST', 
     headers : { 
      "SOAPAction" : "http://tempuri.org/OVCServWeb/OVCCoordenadas/Consulta_CPMRC", 
      "Content-Type": "text/xml; charset=utf-8", 
      "Content-Length": Buffer.byteLength(soapRequest)} 
    }; 

    var httpreq = https.request(options, function (response) { 
     response.setEncoding('utf8'); 
     response.on('data', function (chunk) { 
      console.log("body: " + chunk); 
      res.send(chunk); 
     }); 
     response.on('end', function() { 
      //res.send('ok'); 
     }); 
    }); 

    httpreq.write(soapRequest); 
    httpreq.end(); 
}); 

app.listen(8100, function() { 
    console.log('CORS-enabled web server listening on port 8100') 
}); 

Le service où j'appelle le nœud:

.factory("catastro", function($http, $sce) { 
    /* 
    * INICIALIZACIÓN DE VARIABLES 
    */ 
    var url = 'http://localhost:8100/space/'; 
    var cat = {}; 

    /* 
    * FUNCIONES 
    */ 
    // Obtener XML por referencia catastral 
    cat.leerReferenciaCatastral = function(rc) { 
     var urladd = url + rc; 
     return $http.get(urladd) 
     .then(function(respuesta) { 
      return respuesta.data; 
     }); 
    }; 

    return cat; 
}) 

C'est ma première fois avec NodeJS et je suis un peu perdu. J'ai cherché sur 'https' et demande une fonction à IS ... Une suggestion?

Répondre

1

C'est parce que vous essayez d'appeler la fonction request sur l'objet rootCas:

const https = require('https').globalAgent.options.ca = rootCas; 

En d'autres termes, ici vous assignez rootCas à la fois require('https').globalAgent.options.ca et https.


Essayez plutôt ceci:

const https = require('https'); 
https.globalAgent.options.ca = rootCas;