Calling SOAP services from AngularJS client with Node and Express proxy
Had you every faced the CORS issue while consuming an external web service from an Angularjs client?
Here's a simple solution to overcome the issue.
From angularjs, make a GET call to a local proxy (http://locahost:8100/getData) created in node/express using any of the below 2 methods:
- using request object
var express = require('express');
var bodyparser = require('body-parser');
var querystring = require('querystring');
var request = require('request');
var app = express();
app.use(bodyparser.urlencoded({
extened:false
}));
app.use(bodyparser.json());
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// 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');
// 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('/getData',function(req, res){
// res.send('Hello World!')
//})
app.get('/getData', function (req, res) {
var soapRequest = 'your soap xml request here'
// Basic Authentication credentials, if the wsdl needs credentials
var username = "xxxxxx";
var password = "yyyyyy";
var authenticationHeader = "Basic " + new Buffer(username + ":" + password).toString("base64");
request.post({
uri:"your wsdl url here",
form: soapRequest,
headers : { "Authorization" : authenticationHeader }
},function(err,res1,body){
console.log(body);
console.log(res1.statusCode);
res.send(body);
});
});
app.listen(8100, function () {
console.log('Example app listening on port 8100')
})
- using https object:
var express = require('express');
var bodyparser = require('body-parser');
var querystring = require('querystring');
var https = require('https');
var app = express();
app.use(bodyparser.urlencoded({
extened:false
}));
app.use(bodyparser.json());
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// 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');
// 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('/getData',function(req, res){
// res.send('Hello World!')
//})
app.get('/getData', function (req, res) {
var soapRequest = "your your soap xml request here"
// Basic Authentication credentials, if the wsdl needs credentials
var username = "xxxxxxx";
var password = "yyyyyyy";
var authenticationHeader = "Basic " + new Buffer(username + ":" + password).toString("base64");
var options = {
host: 'webservice host name here',
path: 'webservice the path here',
method: 'POST',
headers :
{
"Authorization" : authenticationHeader,
"Content-Type": "text/xml",
"Content-Length": Buffer.byteLength(soapRequest)
}
};
//console.log(options)
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('Example app listening on port 8100')
})
That's it. You may use te below package.json for your nodejs app:
{
"name": "sample",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.4",
"http": "^0.0.0",
"querystring": "^0.2.0",
"request": "^2.81.0"
},
"devDependencies": {},
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC"
}
Done.....pretty simple, right?