Created
December 12, 2016 11:33
-
-
Save cianclarke/2e3dd494301fe0526176cd368b365ea6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var cors = require('cors'); | |
var soap = require('soap'); | |
var csv = require('csv'); | |
function helloRoute() { | |
var hello = new express.Router(); | |
hello.use(cors()); | |
hello.use(bodyParser()); | |
// GET REST endpoint - query params may or may not be populated | |
hello.all('/', function(req, res) { | |
var barcode = req.query.hello || req.body.hello || "Cian"; | |
var wsdlUrl = 'http://www.searchupc.com/service/UPCSearch.asmx?wsdl'; | |
soap.createClient(wsdlUrl, function(err, soapClient){ | |
if (err){ | |
return res.status(500).json(err); | |
} | |
soapClient.GetProduct({ | |
upc : barcode, | |
accesstoken : '924646BB-A268-4007-9D87-2CE3084B47BC' | |
}, function(err, soapResponse){ | |
if (err){ | |
return res.status(500).json(err); | |
} | |
csv.parse(soapResponse.GetProductResult, { columns : true }, function(err, parsedCsv){ | |
if (err){ | |
return res.status(500).json(err); | |
} | |
if (!parsedCsv || !parsedCsv.length || parsedCsv.length < 1){ | |
return res.status(404).json({ message : "no result found"}); | |
} | |
return res.json({ msg : parsedCsv[0].productname }); | |
}) | |
}) | |
}); | |
}); | |
return hello; | |
} | |
module.exports = helloRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment