I am trying to create a web application that has to make a REST call to a CDAP Server. When I tried using the typical jQuery/AJAX I was running into CORS/Access-Control-Allow-Origin issue due to the obvious reason that this would be a cross domain request. Sadly, CDAP does not support CORS.
Now the only option I am left out with is to create a Server Side proxy. The following is the setup:
The following is the flow of control between browser, Nodejs proxy & CDAP,
Nodejs proxy is running at localhost:8500
CDAP instance is running at localhost:10000
Browser:
Nodejs proxy:
The curl equivalent of this REST call is below:
curl -v localhost:10000/v3/namespaces/default/apps/S3Text -H "Content-Type: application/json" -d @new.json -X PUT
I am new to Node.js but have read some documentation and went through few videos. The point where I am stuck is which part I need to load the JSON file and other parameters like method, data, dataType, etc. Will it be in the JS code or in the Node.js proxy server code. If it has to go in the nodeProxy.js code, then where and how do I need to pass them? My apologies if I am being naive.
JS Code:
function sendCurlRequest(){
var jsonData = <JSON_DATA>;
$.ajax({
cache : false,
method: "PUT",
crossDomain: true,
url: 'http://localhost:8500/rest/v3/namespaces/default/apps/S3Text',
data: jsonData,
dataType: "json",
contentType: "application/json",
success: function(data){
alert("Success");
},
error: function(data){
alert("Error: " + JSON.stringify(data));
},
complete: function(data){
console.log("Call Completed");
}
});
}
nodeProxy.js code:
var http = require('http');
var httpRequest = require('request');
var destinationURL = 'http://localhost:1000/v3/namespaces/default/apps/S3Text';
http.createServer(function (req, res) {
var options = {
url: destinationURL
}
var destinationResponse = req.pipe(request(options))destinationResponse.pipe(res)
}).listen(8500, 'localhost');
console.log('Server running at http://localhost:8500');
If you're willing to use some Node modules then this may work though I don't think it will change your URL from localhost:8500/rest/v3
to localhost:10000/v3
. The /rest/
will stay in.
JS
return $http.get("http://localhost:8500/rest/v3/namespaces/default/apps/S3Text")
return $http.post("http://localhost:8500/rest/v3/namespaces/default/apps/S3Text/other", availReqObj);
Node
var express = require('express');
var app = express();
var cors = require('cors');
var proxy = require('http-proxy-middleware');
var port = process.env.PORT || 8500;
app.use(express.static("" + __dirname));
app.use(cors());
app.use('/rest/v3/namespaces/default/apps/S3Text',
proxy({target: "http://localhost:10000",
changeOrigin: false,
logLevel: 'debug'
})
);
app.use('/rest/v3/namespaces/default/apps/S3Text/other',
proxy({target: "http://localhost:10000",
changeOrigin: false,
logLevel: 'debug'
})
);
app.listen(port, function() {
console.log("Listening at " + port);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.