Skip to content

Instantly share code, notes, and snippets.

@rafaelpatro
Last active April 9, 2017 00:19
Show Gist options
  • Save rafaelpatro/767f59583350364da394a648b390cf03 to your computer and use it in GitHub Desktop.
Save rafaelpatro/767f59583350364da394a648b390cf03 to your computer and use it in GitHub Desktop.
Servidor Node.JS para consulta de CEP nos Correios
var soap = require('soap');
var fs = require('fs');
var app = require('express')();
var privateKey = fs.readFileSync('/etc/apache2/ssl.crt/mydomain.key');
var certificate = fs.readFileSync('/etc/apache2/ssl.crt/87g4d56efa87af4.crt');
var chainFile = fs.readFileSync('/etc/apache2/ssl.crt/gd_bundle-g2-g1.crt');
var credentials = {key: privateKey, cert: certificate, chain: chainFile};
var https = require('https').Server(credentials, app);
var url = "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl";
var options = {
ignoredNamespaces: {
namespaces: ['targetNamespace', 'typedNamespace'],
override: true
}
};
app.get('/', function(req, res){
res.setHeader('Access-Control-Allow-Origin', '*');
var cep = req.query.cep.replace("/\D/", '');
if (cep.length == 8) {
soap.createClient(url, options, function(err, client) {
if (err) {
res.json(false);
} else {
client.consultaCEP({cep:req.query.cep}, function(errCli, result) {
res.json(errCli ? false : result['return']);
});
}
});
} else {
res.json(false);
}
});
https.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment